blob: 98310e68f481efb5f50e200d6fe5592076dce15a [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
17#include "reflection.h"
18
19#include "class_linker.h"
Ian Rogers62d6c772013-02-27 08:32:07 -080020#include "common_throws.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070021#include "dex_file-inl.h"
Elliott Hughes418d20f2011-09-22 14:00:39 -070022#include "jni_internal.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070023#include "mirror/art_field-inl.h"
24#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class.h"
26#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/object_array.h"
28#include "mirror/object_array-inl.h"
Jeff Hao11d5d8f2014-03-26 15:08:20 -070029#include "nth_caller_visitor.h"
Ian Rogers6d4d9fc2011-11-30 16:24:48 -080030#include "object_utils.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:
39 explicit ArgArray(const char* shorty, uint32_t shorty_len)
40 : 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
75 void Append(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
76 Append(StackReference<mirror::Object>::FromMirrorPtr(obj).AsVRegValue());
77 }
78
79 void AppendWide(uint64_t value) {
80 // For ARM and MIPS portable, align wide values to 8 bytes (ArgArray starts at offset of 4).
81#if defined(ART_USE_PORTABLE_COMPILER) && (defined(__arm__) || defined(__mips__))
82 if (num_bytes_ % 8 == 0) {
83 num_bytes_ += 4;
84 }
85#endif
86 arg_array_[num_bytes_ / 4] = value;
87 arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
88 num_bytes_ += 8;
89 }
90
91 void AppendFloat(float value) {
92 jvalue jv;
93 jv.f = value;
94 Append(jv.i);
95 }
96
97 void AppendDouble(double value) {
98 jvalue jv;
99 jv.d = value;
100 AppendWide(jv.j);
101 }
102
Ian Rogerse18fdd22014-03-14 13:29:43 -0700103 void BuildArgArrayFromVarArgs(const ScopedObjectAccess& soa, mirror::Object* receiver, va_list ap)
Ian Rogers53b8b092014-03-13 23:45:53 -0700104 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
105 // Set receiver if non-null (method is not static)
106 if (receiver != nullptr) {
107 Append(receiver);
108 }
109 for (size_t i = 1; i < shorty_len_; ++i) {
110 switch (shorty_[i]) {
111 case 'Z':
112 case 'B':
113 case 'C':
114 case 'S':
115 case 'I':
116 Append(va_arg(ap, jint));
117 break;
118 case 'F':
119 AppendFloat(va_arg(ap, jdouble));
120 break;
121 case 'L':
122 Append(soa.Decode<mirror::Object*>(va_arg(ap, jobject)));
123 break;
124 case 'D':
125 AppendDouble(va_arg(ap, jdouble));
126 break;
127 case 'J':
128 AppendWide(va_arg(ap, jlong));
129 break;
130#ifndef NDEBUG
131 default:
132 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
133#endif
134 }
135 }
136 }
137
Ian Rogerse18fdd22014-03-14 13:29:43 -0700138 void BuildArgArrayFromJValues(const ScopedObjectAccessUnchecked& soa, mirror::Object* receiver,
139 jvalue* args)
Ian Rogers53b8b092014-03-13 23:45:53 -0700140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
141 // Set receiver if non-null (method is not static)
142 if (receiver != nullptr) {
143 Append(receiver);
144 }
145 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
146 switch (shorty_[i]) {
147 case 'Z':
148 Append(args[args_offset].z);
149 break;
150 case 'B':
151 Append(args[args_offset].b);
152 break;
153 case 'C':
154 Append(args[args_offset].c);
155 break;
156 case 'S':
157 Append(args[args_offset].s);
158 break;
159 case 'I':
160 case 'F':
161 Append(args[args_offset].i);
162 break;
163 case 'L':
164 Append(soa.Decode<mirror::Object*>(args[args_offset].l));
165 break;
166 case 'D':
167 case 'J':
168 AppendWide(args[args_offset].j);
169 break;
170#ifndef NDEBUG
171 default:
172 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
173#endif
174 }
175 }
176 }
177
178 void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
179 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
180 // Set receiver if non-null (method is not static)
181 size_t cur_arg = arg_offset;
182 if (!shadow_frame->GetMethod()->IsStatic()) {
183 Append(shadow_frame->GetVReg(cur_arg));
184 cur_arg++;
185 }
186 for (size_t i = 1; i < shorty_len_; ++i) {
187 switch (shorty_[i]) {
188 case 'Z':
189 case 'B':
190 case 'C':
191 case 'S':
192 case 'I':
193 case 'F':
194 case 'L':
195 Append(shadow_frame->GetVReg(cur_arg));
196 cur_arg++;
197 break;
198 case 'D':
199 case 'J':
200 AppendWide(shadow_frame->GetVRegLong(cur_arg));
201 cur_arg++;
202 cur_arg++;
203 break;
204#ifndef NDEBUG
205 default:
206 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
207#endif
208 }
209 }
210 }
211
212 static void ThrowIllegalPrimitiveArgumentException(const char* expected,
213 const StringPiece& found_descriptor)
214 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
215 ThrowIllegalArgumentException(nullptr,
216 StringPrintf("Invalid primitive conversion from %s to %s", expected,
217 PrettyDescriptor(found_descriptor.as_string()).c_str()).c_str());
218 }
219
Ian Rogerse18fdd22014-03-14 13:29:43 -0700220 bool BuildArgArrayFromObjectArray(const ScopedObjectAccess& soa, mirror::Object* receiver,
221 mirror::ObjectArray<mirror::Object>* args, MethodHelper& mh)
Ian Rogers53b8b092014-03-13 23:45:53 -0700222 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
223 const DexFile::TypeList* classes = mh.GetParameterTypeList();
224 // Set receiver if non-null (method is not static)
225 if (receiver != nullptr) {
226 Append(receiver);
227 }
228 for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
229 mirror::Object* arg = args->Get(args_offset);
230 if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) {
231 mirror::Class* dst_class =
232 mh.GetClassFromTypeIdx(classes->GetTypeItem(args_offset).type_idx_);
233 if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
234 ThrowIllegalArgumentException(nullptr,
Ian Rogers11e4c032014-03-14 12:00:39 -0700235 StringPrintf("method %s argument %zd has type %s, got %s",
Ian Rogers53b8b092014-03-13 23:45:53 -0700236 PrettyMethod(mh.GetMethod(), false).c_str(),
237 args_offset + 1, // Humans don't count from 0.
238 PrettyDescriptor(dst_class).c_str(),
239 PrettyTypeOf(arg).c_str()).c_str());
240 return false;
241 }
242 }
243
244#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
245 const StringPiece src_descriptor(arg != nullptr \
246 ? ClassHelper(arg->GetClass<>()).GetDescriptor() \
247 : "null"); \
248 if (LIKELY(src_descriptor == match_descriptor)) { \
249 mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \
250 append(primitive_field-> get_fn(arg));
251
252#define DO_ARG(match_descriptor, get_fn, append) \
253 } else if (LIKELY(src_descriptor == match_descriptor)) { \
254 mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \
255 append(primitive_field-> get_fn(arg));
256
257#define DO_FAIL(expected) \
258 } else { \
259 if (arg->GetClass<>()->IsPrimitive()) { \
260 ThrowIllegalPrimitiveArgumentException(expected, src_descriptor); \
261 } else { \
262 ThrowIllegalArgumentException(nullptr, \
Ian Rogers11e4c032014-03-14 12:00:39 -0700263 StringPrintf("method %s argument %zd has type %s, got %s", \
Ian Rogers53b8b092014-03-13 23:45:53 -0700264 PrettyMethod(mh.GetMethod(), false).c_str(), \
265 args_offset + 1, \
266 expected, \
267 PrettyTypeOf(arg).c_str()).c_str()); \
268 } \
269 return false; \
270 } }
271
272 switch (shorty_[i]) {
273 case 'L':
274 Append(arg);
275 break;
276 case 'Z':
277 DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
278 DO_FAIL("boolean")
279 break;
280 case 'B':
281 DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
282 DO_FAIL("byte")
283 break;
284 case 'C':
285 DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
286 DO_FAIL("char")
287 break;
288 case 'S':
289 DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
290 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
291 DO_FAIL("short")
292 break;
293 case 'I':
294 DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
295 DO_ARG("Ljava/lang/Character;", GetChar, Append)
296 DO_ARG("Ljava/lang/Short;", GetShort, Append)
297 DO_ARG("Ljava/lang/Byte;", GetByte, Append)
298 DO_FAIL("int")
299 break;
300 case 'J':
301 DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
302 DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
303 DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
304 DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
305 DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
306 DO_FAIL("long")
307 break;
308 case 'F':
309 DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
310 DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
311 DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
312 DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
313 DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
314 DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
315 DO_FAIL("float")
316 break;
317 case 'D':
318 DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
319 DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
320 DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
321 DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
322 DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
323 DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
324 DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
325 DO_FAIL("double")
326 break;
327#ifndef NDEBUG
328 default:
329 LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
330#endif
331 }
332#undef DO_FIRST_ARG
333#undef DO_ARG
334#undef DO_FAIL
335 }
336 return true;
337 }
338
339 private:
340 enum { kSmallArgArraySize = 16 };
341 const char* const shorty_;
342 const uint32_t shorty_len_;
343 uint32_t num_bytes_;
344 uint32_t* arg_array_;
345 uint32_t small_arg_array_[kSmallArgArraySize];
346 UniquePtr<uint32_t[]> large_arg_array_;
347};
348
349static void CheckMethodArguments(mirror::ArtMethod* m, uint32_t* args)
350 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
351 const DexFile::TypeList* params = MethodHelper(m).GetParameterTypeList();
352 if (params == nullptr) {
353 return; // No arguments so nothing to check.
354 }
355 uint32_t offset = 0;
356 uint32_t num_params = params->Size();
357 size_t error_count = 0;
358 if (!m->IsStatic()) {
359 offset = 1;
360 }
361 for (uint32_t i = 0; i < num_params; i++) {
362 uint16_t type_idx = params->GetTypeItem(i).type_idx_;
363 mirror::Class* param_type = MethodHelper(m).GetClassFromTypeIdx(type_idx);
364 if (param_type == nullptr) {
365 Thread* self = Thread::Current();
366 CHECK(self->IsExceptionPending());
367 LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
368 << MethodHelper(m).GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
369 << self->GetException(nullptr)->Dump();
370 self->ClearException();
371 ++error_count;
372 } else if (!param_type->IsPrimitive()) {
373 // TODO: check primitives are in range.
374 mirror::Object* argument = reinterpret_cast<mirror::Object*>(args[i + offset]);
375 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)
378 << " to " << PrettyMethod(m);
379 ++error_count;
380 }
381 } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
382 offset++;
383 }
384 }
385 if (error_count > 0) {
386 // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
387 // with an argument.
388 JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
389 PrettyMethod(m).c_str());
390 }
391}
392
393static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver,
394 mirror::ArtMethod* method)
395 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
396 return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
397}
398
399
400static void InvokeWithArgArray(const ScopedObjectAccessUnchecked& soa, mirror::ArtMethod* method,
401 ArgArray* arg_array, JValue* result, const char* shorty)
402 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
403 uint32_t* args = arg_array->GetArray();
404 if (UNLIKELY(soa.Env()->check_jni)) {
405 CheckMethodArguments(method, args);
406 }
407 method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
408}
409
410JValue InvokeWithVarArgs(const ScopedObjectAccess& soa, jobject obj, jmethodID mid, va_list args)
411 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
412 mirror::ArtMethod* method = soa.DecodeMethod(mid);
413 mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
414 MethodHelper mh(method);
415 JValue result;
416 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogerse18fdd22014-03-14 13:29:43 -0700417 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700418 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
419 return result;
420}
421
422JValue InvokeWithJValues(const ScopedObjectAccessUnchecked& soa, mirror::Object* receiver,
423 jmethodID mid, jvalue* args) {
424 mirror::ArtMethod* method = soa.DecodeMethod(mid);
425 MethodHelper mh(method);
426 JValue result;
427 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogerse18fdd22014-03-14 13:29:43 -0700428 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700429 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
430 return result;
431}
432
433JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccess& soa,
434 mirror::Object* receiver, jmethodID mid, jvalue* args) {
435 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
436 MethodHelper mh(method);
437 JValue result;
438 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogerse18fdd22014-03-14 13:29:43 -0700439 arg_array.BuildArgArrayFromJValues(soa, receiver, args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700440 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
441 return result;
442}
443
444JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccess& soa,
445 jobject obj, jmethodID mid, va_list args) {
446 mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
447 mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
448 MethodHelper mh(method);
449 JValue result;
450 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogerse18fdd22014-03-14 13:29:43 -0700451 arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
Ian Rogers53b8b092014-03-13 23:45:53 -0700452 InvokeWithArgArray(soa, method, &arg_array, &result, mh.GetShorty());
453 return result;
454}
455
456void InvokeWithShadowFrame(Thread* self, ShadowFrame* shadow_frame, uint16_t arg_offset,
457 MethodHelper& mh, JValue* result) {
458 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
459 arg_array.BuildArgArrayFromFrame(shadow_frame, arg_offset);
460 shadow_frame->GetMethod()->Invoke(self, arg_array.GetArray(), arg_array.GetNumBytes(), result,
461 mh.GetShorty());
462}
463
464jobject InvokeMethod(const ScopedObjectAccess& soa, jobject javaMethod,
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700465 jobject javaReceiver, jobject javaArgs, bool accessible) {
Ian Rogers62f05122014-03-21 11:21:29 -0700466 mirror::ArtMethod* m = mirror::ArtMethod::FromReflectedMethod(soa, javaMethod);
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700467
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800468 mirror::Class* declaring_class = m->GetDeclaringClass();
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800469 if (UNLIKELY(!declaring_class->IsInitialized())) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700470 StackHandleScope<1> hs(soa.Self());
471 Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
472 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(h_class, true, true)) {
Mathieu Chartierc528dba2013-11-26 12:00:11 -0800473 return nullptr;
474 }
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700475 declaring_class = h_class.Get();
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700476 }
477
Ian Rogers53b8b092014-03-13 23:45:53 -0700478 mirror::Object* receiver = nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700479 if (!m->IsStatic()) {
480 // Check that the receiver is non-null and an instance of the field's declaring class.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 receiver = soa.Decode<mirror::Object*>(javaReceiver);
Ian Rogers53b8b092014-03-13 23:45:53 -0700482 if (!VerifyObjectIsClass(receiver, declaring_class)) {
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700483 return NULL;
484 }
485
486 // Find the actual implementation of the virtual method.
487 m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
488 }
489
490 // Get our arrays of arguments and their types, and check they're the same size.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800491 mirror::ObjectArray<mirror::Object>* objects =
492 soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800493 MethodHelper mh(m);
494 const DexFile::TypeList* classes = mh.GetParameterTypeList();
Ian Rogers53b8b092014-03-13 23:45:53 -0700495 uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
496 uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800497 if (arg_count != classes_size) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800498 ThrowIllegalArgumentException(NULL,
499 StringPrintf("Wrong number of arguments; expected %d, got %d",
500 classes_size, arg_count).c_str());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700501 return NULL;
502 }
503
Jeff Haocb4581a2014-03-28 15:43:37 -0700504 // If method is not set to be accessible, verify it can be accessed by the caller.
505 if (!accessible && !VerifyAccess(receiver, declaring_class, m->GetAccessFlags())) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700506 ThrowIllegalAccessException(nullptr, StringPrintf("Cannot access method: %s",
507 PrettyMethod(m).c_str()).c_str());
508 return nullptr;
509 }
510
Ian Rogers53b8b092014-03-13 23:45:53 -0700511 // Invoke the method.
512 JValue result;
513 ArgArray arg_array(mh.GetShorty(), mh.GetShortyLength());
Ian Rogerse18fdd22014-03-14 13:29:43 -0700514 if (!arg_array.BuildArgArrayFromObjectArray(soa, receiver, objects, mh)) {
Ian Rogers53b8b092014-03-13 23:45:53 -0700515 CHECK(soa.Self()->IsExceptionPending());
516 return nullptr;
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700517 }
518
Ian Rogers53b8b092014-03-13 23:45:53 -0700519 InvokeWithArgArray(soa, m, &arg_array, &result, mh.GetShorty());
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700520
521 // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700522 if (soa.Self()->IsExceptionPending()) {
523 jthrowable th = soa.Env()->ExceptionOccurred();
524 soa.Env()->ExceptionClear();
525 jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
526 jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
527 jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
528 soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700529 return NULL;
530 }
531
532 // Box if necessary and return.
Ian Rogers53b8b092014-03-13 23:45:53 -0700533 return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(),
534 result));
Elliott Hughes2a20cfd2011-09-23 19:30:41 -0700535}
536
Ian Rogers53b8b092014-03-13 23:45:53 -0700537bool VerifyObjectIsClass(mirror::Object* o, mirror::Class* c) {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700538 if (o == NULL) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800539 ThrowNullPointerException(NULL, "null receiver");
540 return false;
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700541 } else if (!o->InstanceOf(c)) {
Elliott Hughesb600b3f2012-03-14 13:57:24 -0700542 std::string expected_class_name(PrettyDescriptor(c));
543 std::string actual_class_name(PrettyTypeOf(o));
Ian Rogers62d6c772013-02-27 08:32:07 -0800544 ThrowIllegalArgumentException(NULL,
545 StringPrintf("Expected receiver of type %s, but got %s",
546 expected_class_name.c_str(),
547 actual_class_name.c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700548 return false;
549 }
550 return true;
551}
552
Ian Rogers62d6c772013-02-27 08:32:07 -0800553bool ConvertPrimitiveValue(const ThrowLocation* throw_location, bool unbox_for_result,
554 Primitive::Type srcType, Primitive::Type dstType,
Ian Rogers84956ff2014-03-26 23:52:41 -0700555 const JValue& src, JValue* dst) {
556 DCHECK(srcType != Primitive::kPrimNot && dstType != Primitive::kPrimNot);
557 if (LIKELY(srcType == dstType)) {
558 dst->SetJ(src.GetJ());
559 return true;
560 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700561 switch (dstType) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700562 case Primitive::kPrimBoolean: // Fall-through.
563 case Primitive::kPrimChar: // Fall-through.
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700564 case Primitive::kPrimByte:
Ian Rogers84956ff2014-03-26 23:52:41 -0700565 // Only expect assignment with source and destination of identical type.
Elliott Hughes418d20f2011-09-22 14:00:39 -0700566 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700567 case Primitive::kPrimShort:
Ian Rogers84956ff2014-03-26 23:52:41 -0700568 if (srcType == Primitive::kPrimByte) {
569 dst->SetS(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700570 return true;
571 }
572 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700573 case Primitive::kPrimInt:
574 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
Ian Rogers84956ff2014-03-26 23:52:41 -0700575 srcType == Primitive::kPrimShort) {
576 dst->SetI(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700577 return true;
578 }
579 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700580 case Primitive::kPrimLong:
581 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
582 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700583 dst->SetJ(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700584 return true;
585 }
586 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700587 case Primitive::kPrimFloat:
588 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
589 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700590 dst->SetF(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700591 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700592 } else if (srcType == Primitive::kPrimLong) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700593 dst->SetF(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700594 return true;
595 }
596 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700597 case Primitive::kPrimDouble:
598 if (srcType == Primitive::kPrimByte || srcType == Primitive::kPrimChar ||
599 srcType == Primitive::kPrimShort || srcType == Primitive::kPrimInt) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700600 dst->SetD(src.GetI());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700601 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700602 } else if (srcType == Primitive::kPrimLong) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700603 dst->SetD(src.GetJ());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700604 return true;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700605 } else if (srcType == Primitive::kPrimFloat) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700606 dst->SetD(src.GetF());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700607 return true;
608 }
609 break;
610 default:
611 break;
612 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800613 if (!unbox_for_result) {
614 ThrowIllegalArgumentException(throw_location,
615 StringPrintf("Invalid primitive conversion from %s to %s",
616 PrettyDescriptor(srcType).c_str(),
617 PrettyDescriptor(dstType).c_str()).c_str());
618 } else {
619 ThrowClassCastException(throw_location,
620 StringPrintf("Couldn't convert result of type %s to %s",
621 PrettyDescriptor(srcType).c_str(),
Brian Carlstromdf629502013-07-17 22:39:56 -0700622 PrettyDescriptor(dstType).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800623 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700624 return false;
625}
626
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800627mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700628 if (src_class == Primitive::kPrimNot) {
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800629 return value.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700630 }
Ian Rogers53b8b092014-03-13 23:45:53 -0700631 if (src_class == Primitive::kPrimVoid) {
632 // There's no such thing as a void field, and void methods invoked via reflection return null.
633 return nullptr;
634 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700635
Ian Rogers84956ff2014-03-26 23:52:41 -0700636 jmethodID m = nullptr;
Ian Rogers0177e532014-02-11 16:30:46 -0800637 const char* shorty;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700638 switch (src_class) {
639 case Primitive::kPrimBoolean:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700640 m = WellKnownClasses::java_lang_Boolean_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800641 shorty = "LZ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700642 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700643 case Primitive::kPrimByte:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700644 m = WellKnownClasses::java_lang_Byte_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800645 shorty = "LB";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700646 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700647 case Primitive::kPrimChar:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700648 m = WellKnownClasses::java_lang_Character_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800649 shorty = "LC";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700650 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700651 case Primitive::kPrimDouble:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700652 m = WellKnownClasses::java_lang_Double_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800653 shorty = "LD";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700654 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700655 case Primitive::kPrimFloat:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700656 m = WellKnownClasses::java_lang_Float_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800657 shorty = "LF";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700658 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700659 case Primitive::kPrimInt:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700660 m = WellKnownClasses::java_lang_Integer_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800661 shorty = "LI";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700662 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700663 case Primitive::kPrimLong:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700664 m = WellKnownClasses::java_lang_Long_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800665 shorty = "LJ";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700666 break;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700667 case Primitive::kPrimShort:
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700668 m = WellKnownClasses::java_lang_Short_valueOf;
Ian Rogers0177e532014-02-11 16:30:46 -0800669 shorty = "LS";
Elliott Hughes418d20f2011-09-22 14:00:39 -0700670 break;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700671 default:
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700672 LOG(FATAL) << static_cast<int>(src_class);
Ian Rogers0177e532014-02-11 16:30:46 -0800673 shorty = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700674 }
675
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700676 ScopedObjectAccessUnchecked soa(Thread::Current());
Ian Rogers53b8b092014-03-13 23:45:53 -0700677 DCHECK_EQ(soa.Self()->GetState(), kRunnable);
Jeff Hao5d917302013-02-27 17:57:33 -0800678
Ian Rogers53b8b092014-03-13 23:45:53 -0700679 ArgArray arg_array(shorty, 2);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800680 JValue result;
Jeff Hao5d917302013-02-27 17:57:33 -0800681 if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
682 arg_array.AppendWide(value.GetJ());
683 } else {
684 arg_array.Append(value.GetI());
685 }
686
687 soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
Ian Rogers0177e532014-02-11 16:30:46 -0800688 &result, shorty);
Ian Rogersaf6e67a2013-01-16 08:38:37 -0800689 return result.GetL();
Elliott Hughes418d20f2011-09-22 14:00:39 -0700690}
691
Ian Rogers84956ff2014-03-26 23:52:41 -0700692static std::string UnboxingFailureKind(mirror::ArtField* f)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700693 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700694 if (f != nullptr) {
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700695 return "field " + PrettyField(f, false);
696 }
697 return "result";
698}
699
Ian Rogers62d6c772013-02-27 08:32:07 -0800700static bool UnboxPrimitive(const ThrowLocation* throw_location, mirror::Object* o,
Ian Rogers84956ff2014-03-26 23:52:41 -0700701 mirror::Class* dst_class, mirror::ArtField* f,
702 JValue* unboxed_value)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700703 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700704 bool unbox_for_result = (f == nullptr);
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700705 if (!dst_class->IsPrimitive()) {
Ian Rogers84956ff2014-03-26 23:52:41 -0700706 if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800707 if (!unbox_for_result) {
708 ThrowIllegalArgumentException(throw_location,
709 StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700710 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800711 PrettyDescriptor(dst_class).c_str(),
712 PrettyTypeOf(o).c_str()).c_str());
713 } else {
714 ThrowClassCastException(throw_location,
715 StringPrintf("Couldn't convert result of type %s to %s",
716 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)) {
725 ThrowIllegalArgumentException(throw_location,
726 StringPrintf("Can't unbox %s to void",
Ian Rogers84956ff2014-03-26 23:52:41 -0700727 UnboxingFailureKind(f).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700728 return false;
729 }
Ian Rogers84956ff2014-03-26 23:52:41 -0700730 if (UNLIKELY(o == nullptr)) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800731 if (!unbox_for_result) {
732 ThrowIllegalArgumentException(throw_location,
733 StringPrintf("%s has type %s, got null",
Ian Rogers84956ff2014-03-26 23:52:41 -0700734 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800735 PrettyDescriptor(dst_class).c_str()).c_str());
736 } else {
737 ThrowNullPointerException(throw_location,
738 StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
739 PrettyDescriptor(dst_class).c_str()).c_str());
740 }
Elliott Hughes418d20f2011-09-22 14:00:39 -0700741 return false;
742 }
743
Elliott Hughes1d878f32012-04-11 15:17:54 -0700744 JValue boxed_value;
Ian Rogersdfb325e2013-10-30 01:00:44 -0700745 const StringPiece src_descriptor(ClassHelper(o->GetClass()).GetDescriptor());
Ian Rogers84956ff2014-03-26 23:52:41 -0700746 mirror::Class* src_class = nullptr;
Elliott Hughes418d20f2011-09-22 14:00:39 -0700747 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Brian Carlstromea46f952013-07-30 01:26:50 -0700748 mirror::ArtField* primitive_field = o->GetClass()->GetIFields()->Get(0);
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800749 if (src_descriptor == "Ljava/lang/Boolean;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700750 src_class = class_linker->FindPrimitiveClass('Z');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700751 boxed_value.SetZ(primitive_field->GetBoolean(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800752 } else if (src_descriptor == "Ljava/lang/Byte;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700753 src_class = class_linker->FindPrimitiveClass('B');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700754 boxed_value.SetB(primitive_field->GetByte(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800755 } else if (src_descriptor == "Ljava/lang/Character;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700756 src_class = class_linker->FindPrimitiveClass('C');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700757 boxed_value.SetC(primitive_field->GetChar(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800758 } else if (src_descriptor == "Ljava/lang/Float;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700759 src_class = class_linker->FindPrimitiveClass('F');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700760 boxed_value.SetF(primitive_field->GetFloat(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800761 } else if (src_descriptor == "Ljava/lang/Double;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700762 src_class = class_linker->FindPrimitiveClass('D');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700763 boxed_value.SetD(primitive_field->GetDouble(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800764 } else if (src_descriptor == "Ljava/lang/Integer;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700765 src_class = class_linker->FindPrimitiveClass('I');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700766 boxed_value.SetI(primitive_field->GetInt(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800767 } else if (src_descriptor == "Ljava/lang/Long;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700768 src_class = class_linker->FindPrimitiveClass('J');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700769 boxed_value.SetJ(primitive_field->GetLong(o));
Ian Rogers6d4d9fc2011-11-30 16:24:48 -0800770 } else if (src_descriptor == "Ljava/lang/Short;") {
Elliott Hughes418d20f2011-09-22 14:00:39 -0700771 src_class = class_linker->FindPrimitiveClass('S');
Elliott Hughesf24d3ce2012-04-11 17:43:37 -0700772 boxed_value.SetS(primitive_field->GetShort(o));
Elliott Hughes418d20f2011-09-22 14:00:39 -0700773 } else {
Ian Rogers62d6c772013-02-27 08:32:07 -0800774 ThrowIllegalArgumentException(throw_location,
775 StringPrintf("%s has type %s, got %s",
Ian Rogers84956ff2014-03-26 23:52:41 -0700776 UnboxingFailureKind(f).c_str(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800777 PrettyDescriptor(dst_class).c_str(),
Ian Rogersfc0e94b2013-09-23 23:51:32 -0700778 PrettyDescriptor(src_descriptor.data()).c_str()).c_str());
Elliott Hughes418d20f2011-09-22 14:00:39 -0700779 return false;
780 }
781
Ian Rogers62d6c772013-02-27 08:32:07 -0800782 return ConvertPrimitiveValue(throw_location, unbox_for_result,
783 src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700784 boxed_value, unboxed_value);
Elliott Hughes418d20f2011-09-22 14:00:39 -0700785}
786
Ian Rogers84956ff2014-03-26 23:52:41 -0700787bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, mirror::ArtField* f,
788 JValue* unboxed_value) {
789 DCHECK(f != nullptr);
790 return UnboxPrimitive(nullptr, o, dst_class, f, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700791}
792
Ian Rogers62d6c772013-02-27 08:32:07 -0800793bool UnboxPrimitiveForResult(const ThrowLocation& throw_location, mirror::Object* o,
Ian Rogers84956ff2014-03-26 23:52:41 -0700794 mirror::Class* dst_class, JValue* unboxed_value) {
795 return UnboxPrimitive(&throw_location, o, dst_class, nullptr, unboxed_value);
Elliott Hughesaaa5edc2012-05-16 15:54:30 -0700796}
797
Jeff Haocb4581a2014-03-28 15:43:37 -0700798bool VerifyAccess(mirror::Object* obj, mirror::Class* declaring_class, uint32_t access_flags) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700799 NthCallerVisitor visitor(Thread::Current(), 2);
800 visitor.WalkStack();
801 mirror::Class* caller_class = visitor.caller->GetDeclaringClass();
802
Jeff Hao925b6872014-04-01 11:21:30 -0700803 if (((access_flags & kAccPublic) != 0) || (caller_class == declaring_class)) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700804 return true;
805 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700806 if ((access_flags & kAccPrivate) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700807 return false;
808 }
Jeff Haocb4581a2014-03-28 15:43:37 -0700809 if ((access_flags & kAccProtected) != 0) {
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700810 if (obj != nullptr && !obj->InstanceOf(caller_class) &&
811 !declaring_class->IsInSamePackage(caller_class)) {
812 return false;
813 } else if (declaring_class->IsAssignableFrom(caller_class)) {
814 return true;
815 }
816 }
817 if (!declaring_class->IsInSamePackage(caller_class)) {
818 return false;
819 }
820 return true;
821}
822
Elliott Hughes418d20f2011-09-22 14:00:39 -0700823} // namespace art