reflection.cc revision 6f3dbbadf4ce66982eb3d400e0a74cb73eb034f3
1/*
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-inl.h"
18
19#include "class_linker.h"
20#include "common_throws.h"
21#include "dex_file-inl.h"
22#include "entrypoints/entrypoint_utils.h"
23#include "jni_internal.h"
24#include "method_helper-inl.h"
25#include "mirror/art_field-inl.h"
26#include "mirror/art_method-inl.h"
27#include "mirror/class-inl.h"
28#include "mirror/class.h"
29#include "mirror/object_array-inl.h"
30#include "mirror/object_array.h"
31#include "nth_caller_visitor.h"
32#include "scoped_thread_state_change.h"
33#include "stack.h"
34#include "well_known_classes.h"
35
36namespace art {
37
38class ArgArray {
39 public:
40  explicit ArgArray(const char* shorty, uint32_t shorty_len)
41      : shorty_(shorty), shorty_len_(shorty_len), num_bytes_(0) {
42    size_t num_slots = shorty_len + 1;  // +1 in case of receiver.
43    if (LIKELY((num_slots * 2) < kSmallArgArraySize)) {
44      // We can trivially use the small arg array.
45      arg_array_ = small_arg_array_;
46    } else {
47      // Analyze shorty to see if we need the large arg array.
48      for (size_t i = 1; i < shorty_len; ++i) {
49        char c = shorty[i];
50        if (c == 'J' || c == 'D') {
51          num_slots++;
52        }
53      }
54      if (num_slots <= kSmallArgArraySize) {
55        arg_array_ = small_arg_array_;
56      } else {
57        large_arg_array_.reset(new uint32_t[num_slots]);
58        arg_array_ = large_arg_array_.get();
59      }
60    }
61  }
62
63  uint32_t* GetArray() {
64    return arg_array_;
65  }
66
67  uint32_t GetNumBytes() {
68    return num_bytes_;
69  }
70
71  void Append(uint32_t value) {
72    arg_array_[num_bytes_ / 4] = value;
73    num_bytes_ += 4;
74  }
75
76  void Append(mirror::Object* obj) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
77    Append(StackReference<mirror::Object>::FromMirrorPtr(obj).AsVRegValue());
78  }
79
80  void AppendWide(uint64_t value) {
81    // For ARM and MIPS portable, align wide values to 8 bytes (ArgArray starts at offset of 4).
82#if defined(ART_USE_PORTABLE_COMPILER) && (defined(__arm__) || defined(__mips__))
83    if (num_bytes_ % 8 == 0) {
84      num_bytes_ += 4;
85    }
86#endif
87    arg_array_[num_bytes_ / 4] = value;
88    arg_array_[(num_bytes_ / 4) + 1] = value >> 32;
89    num_bytes_ += 8;
90  }
91
92  void AppendFloat(float value) {
93    jvalue jv;
94    jv.f = value;
95    Append(jv.i);
96  }
97
98  void AppendDouble(double value) {
99    jvalue jv;
100    jv.d = value;
101    AppendWide(jv.j);
102  }
103
104  void BuildArgArrayFromVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
105                                mirror::Object* receiver, va_list ap)
106      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
107    // Set receiver if non-null (method is not static)
108    if (receiver != nullptr) {
109      Append(receiver);
110    }
111    for (size_t i = 1; i < shorty_len_; ++i) {
112      switch (shorty_[i]) {
113        case 'Z':
114        case 'B':
115        case 'C':
116        case 'S':
117        case 'I':
118          Append(va_arg(ap, jint));
119          break;
120        case 'F':
121          AppendFloat(va_arg(ap, jdouble));
122          break;
123        case 'L':
124          Append(soa.Decode<mirror::Object*>(va_arg(ap, jobject)));
125          break;
126        case 'D':
127          AppendDouble(va_arg(ap, jdouble));
128          break;
129        case 'J':
130          AppendWide(va_arg(ap, jlong));
131          break;
132#ifndef NDEBUG
133        default:
134          LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
135#endif
136      }
137    }
138  }
139
140  void BuildArgArrayFromJValues(const ScopedObjectAccessAlreadyRunnable& soa,
141                                mirror::Object* receiver, jvalue* args)
142      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
143    // Set receiver if non-null (method is not static)
144    if (receiver != nullptr) {
145      Append(receiver);
146    }
147    for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
148      switch (shorty_[i]) {
149        case 'Z':
150          Append(args[args_offset].z);
151          break;
152        case 'B':
153          Append(args[args_offset].b);
154          break;
155        case 'C':
156          Append(args[args_offset].c);
157          break;
158        case 'S':
159          Append(args[args_offset].s);
160          break;
161        case 'I':
162        case 'F':
163          Append(args[args_offset].i);
164          break;
165        case 'L':
166          Append(soa.Decode<mirror::Object*>(args[args_offset].l));
167          break;
168        case 'D':
169        case 'J':
170          AppendWide(args[args_offset].j);
171          break;
172#ifndef NDEBUG
173        default:
174          LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
175#endif
176      }
177    }
178  }
179
180  void BuildArgArrayFromFrame(ShadowFrame* shadow_frame, uint32_t arg_offset)
181      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
182    // Set receiver if non-null (method is not static)
183    size_t cur_arg = arg_offset;
184    if (!shadow_frame->GetMethod()->IsStatic()) {
185      Append(shadow_frame->GetVReg(cur_arg));
186      cur_arg++;
187    }
188    for (size_t i = 1; i < shorty_len_; ++i) {
189      switch (shorty_[i]) {
190        case 'Z':
191        case 'B':
192        case 'C':
193        case 'S':
194        case 'I':
195        case 'F':
196        case 'L':
197          Append(shadow_frame->GetVReg(cur_arg));
198          cur_arg++;
199          break;
200        case 'D':
201        case 'J':
202          AppendWide(shadow_frame->GetVRegLong(cur_arg));
203          cur_arg++;
204          cur_arg++;
205          break;
206#ifndef NDEBUG
207        default:
208          LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
209#endif
210      }
211    }
212  }
213
214  static void ThrowIllegalPrimitiveArgumentException(const char* expected,
215                                                     const char* found_descriptor)
216      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
217    ThrowIllegalArgumentException(nullptr,
218        StringPrintf("Invalid primitive conversion from %s to %s", expected,
219                     PrettyDescriptor(found_descriptor).c_str()).c_str());
220  }
221
222  bool BuildArgArrayFromObjectArray(const ScopedObjectAccessAlreadyRunnable& soa,
223                                    mirror::Object* receiver,
224                                    mirror::ObjectArray<mirror::Object>* args, MethodHelper& mh)
225      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
226    const DexFile::TypeList* classes = mh.GetMethod()->GetParameterTypeList();
227    // Set receiver if non-null (method is not static)
228    if (receiver != nullptr) {
229      Append(receiver);
230    }
231    for (size_t i = 1, args_offset = 0; i < shorty_len_; ++i, ++args_offset) {
232      mirror::Object* arg = args->Get(args_offset);
233      if (((shorty_[i] == 'L') && (arg != nullptr)) || ((arg == nullptr && shorty_[i] != 'L'))) {
234        mirror::Class* dst_class =
235            mh.GetClassFromTypeIdx(classes->GetTypeItem(args_offset).type_idx_);
236        if (UNLIKELY(arg == nullptr || !arg->InstanceOf(dst_class))) {
237          ThrowIllegalArgumentException(nullptr,
238              StringPrintf("method %s argument %zd has type %s, got %s",
239                  PrettyMethod(mh.GetMethod(), false).c_str(),
240                  args_offset + 1,  // Humans don't count from 0.
241                  PrettyDescriptor(dst_class).c_str(),
242                  PrettyTypeOf(arg).c_str()).c_str());
243          return false;
244        }
245      }
246
247#define DO_FIRST_ARG(match_descriptor, get_fn, append) { \
248          if (LIKELY(arg != nullptr && arg->GetClass<>()->DescriptorEquals(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(arg != nullptr && \
254                            arg->GetClass<>()->DescriptorEquals(match_descriptor))) { \
255            mirror::ArtField* primitive_field = arg->GetClass()->GetIFields()->Get(0); \
256            append(primitive_field-> get_fn(arg));
257
258#define DO_FAIL(expected) \
259          } else { \
260            if (arg->GetClass<>()->IsPrimitive()) { \
261              std::string temp; \
262              ThrowIllegalPrimitiveArgumentException(expected, \
263                                                     arg->GetClass<>()->GetDescriptor(&temp)); \
264            } else { \
265              ThrowIllegalArgumentException(nullptr, \
266                  StringPrintf("method %s argument %zd has type %s, got %s", \
267                      PrettyMethod(mh.GetMethod(), false).c_str(), \
268                      args_offset + 1, \
269                      expected, \
270                      PrettyTypeOf(arg).c_str()).c_str()); \
271            } \
272            return false; \
273          } }
274
275      switch (shorty_[i]) {
276        case 'L':
277          Append(arg);
278          break;
279        case 'Z':
280          DO_FIRST_ARG("Ljava/lang/Boolean;", GetBoolean, Append)
281          DO_FAIL("boolean")
282          break;
283        case 'B':
284          DO_FIRST_ARG("Ljava/lang/Byte;", GetByte, Append)
285          DO_FAIL("byte")
286          break;
287        case 'C':
288          DO_FIRST_ARG("Ljava/lang/Character;", GetChar, Append)
289          DO_FAIL("char")
290          break;
291        case 'S':
292          DO_FIRST_ARG("Ljava/lang/Short;", GetShort, Append)
293          DO_ARG("Ljava/lang/Byte;", GetByte, Append)
294          DO_FAIL("short")
295          break;
296        case 'I':
297          DO_FIRST_ARG("Ljava/lang/Integer;", GetInt, Append)
298          DO_ARG("Ljava/lang/Character;", GetChar, Append)
299          DO_ARG("Ljava/lang/Short;", GetShort, Append)
300          DO_ARG("Ljava/lang/Byte;", GetByte, Append)
301          DO_FAIL("int")
302          break;
303        case 'J':
304          DO_FIRST_ARG("Ljava/lang/Long;", GetLong, AppendWide)
305          DO_ARG("Ljava/lang/Integer;", GetInt, AppendWide)
306          DO_ARG("Ljava/lang/Character;", GetChar, AppendWide)
307          DO_ARG("Ljava/lang/Short;", GetShort, AppendWide)
308          DO_ARG("Ljava/lang/Byte;", GetByte, AppendWide)
309          DO_FAIL("long")
310          break;
311        case 'F':
312          DO_FIRST_ARG("Ljava/lang/Float;", GetFloat, AppendFloat)
313          DO_ARG("Ljava/lang/Long;", GetLong, AppendFloat)
314          DO_ARG("Ljava/lang/Integer;", GetInt, AppendFloat)
315          DO_ARG("Ljava/lang/Character;", GetChar, AppendFloat)
316          DO_ARG("Ljava/lang/Short;", GetShort, AppendFloat)
317          DO_ARG("Ljava/lang/Byte;", GetByte, AppendFloat)
318          DO_FAIL("float")
319          break;
320        case 'D':
321          DO_FIRST_ARG("Ljava/lang/Double;", GetDouble, AppendDouble)
322          DO_ARG("Ljava/lang/Float;", GetFloat, AppendDouble)
323          DO_ARG("Ljava/lang/Long;", GetLong, AppendDouble)
324          DO_ARG("Ljava/lang/Integer;", GetInt, AppendDouble)
325          DO_ARG("Ljava/lang/Character;", GetChar, AppendDouble)
326          DO_ARG("Ljava/lang/Short;", GetShort, AppendDouble)
327          DO_ARG("Ljava/lang/Byte;", GetByte, AppendDouble)
328          DO_FAIL("double")
329          break;
330#ifndef NDEBUG
331        default:
332          LOG(FATAL) << "Unexpected shorty character: " << shorty_[i];
333#endif
334      }
335#undef DO_FIRST_ARG
336#undef DO_ARG
337#undef DO_FAIL
338    }
339    return true;
340  }
341
342 private:
343  enum { kSmallArgArraySize = 16 };
344  const char* const shorty_;
345  const uint32_t shorty_len_;
346  uint32_t num_bytes_;
347  uint32_t* arg_array_;
348  uint32_t small_arg_array_[kSmallArgArraySize];
349  std::unique_ptr<uint32_t[]> large_arg_array_;
350};
351
352static void CheckMethodArguments(JavaVMExt* vm, mirror::ArtMethod* m, uint32_t* args)
353    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
354  const DexFile::TypeList* params = m->GetParameterTypeList();
355  if (params == nullptr) {
356    return;  // No arguments so nothing to check.
357  }
358  uint32_t offset = 0;
359  uint32_t num_params = params->Size();
360  size_t error_count = 0;
361  if (!m->IsStatic()) {
362    offset = 1;
363  }
364  // TODO: If args contain object references, it may cause problems
365  Thread* self = Thread::Current();
366  StackHandleScope<1> hs(self);
367  Handle<mirror::ArtMethod> h_m(hs.NewHandle(m));
368  MethodHelper mh(h_m);
369  for (uint32_t i = 0; i < num_params; i++) {
370    uint16_t type_idx = params->GetTypeItem(i).type_idx_;
371    mirror::Class* param_type = mh.GetClassFromTypeIdx(type_idx);
372    if (param_type == nullptr) {
373      CHECK(self->IsExceptionPending());
374      LOG(ERROR) << "Internal error: unresolvable type for argument type in JNI invoke: "
375          << h_m->GetTypeDescriptorFromTypeIdx(type_idx) << "\n"
376          << self->GetException(nullptr)->Dump();
377      self->ClearException();
378      ++error_count;
379    } else if (!param_type->IsPrimitive()) {
380      // TODO: There is a compaction bug here since GetClassFromTypeIdx can cause thread suspension,
381      // this is a hard to fix problem since the args can contain Object*, we need to save and
382      // restore them by using a visitor similar to the ones used in the trampoline entrypoints.
383      mirror::Object* argument =
384          (reinterpret_cast<StackReference<mirror::Object>*>(&args[i + offset]))->AsMirrorPtr();
385      if (argument != nullptr && !argument->InstanceOf(param_type)) {
386        LOG(ERROR) << "JNI ERROR (app bug): attempt to pass an instance of "
387                   << PrettyTypeOf(argument) << " as argument " << (i + 1)
388                   << " to " << PrettyMethod(h_m.Get());
389        ++error_count;
390      }
391    } else if (param_type->IsPrimitiveLong() || param_type->IsPrimitiveDouble()) {
392      offset++;
393    } else {
394      int32_t arg = static_cast<int32_t>(args[i + offset]);
395      if (param_type->IsPrimitiveBoolean()) {
396        if (arg != JNI_TRUE && arg != JNI_FALSE) {
397          LOG(ERROR) << "JNI ERROR (app bug): expected jboolean (0/1) but got value of "
398              << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
399          ++error_count;
400        }
401      } else if (param_type->IsPrimitiveByte()) {
402        if (arg < -128 || arg > 127) {
403          LOG(ERROR) << "JNI ERROR (app bug): expected jbyte but got value of "
404              << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
405          ++error_count;
406        }
407      } else if (param_type->IsPrimitiveChar()) {
408        if (args[i + offset] > 0xFFFF) {
409          LOG(ERROR) << "JNI ERROR (app bug): expected jchar but got value of "
410              << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
411          ++error_count;
412        }
413      } else if (param_type->IsPrimitiveShort()) {
414        if (arg < -32768 || arg > 0x7FFF) {
415          LOG(ERROR) << "JNI ERROR (app bug): expected jshort but got value of "
416              << arg << " as argument " << (i + 1) << " to " << PrettyMethod(h_m.Get());
417          ++error_count;
418        }
419      }
420    }
421  }
422  if (UNLIKELY(error_count > 0)) {
423    // TODO: pass the JNI function name (such as "CallVoidMethodV") through so we can call JniAbort
424    // with an argument.
425    vm->JniAbortF(nullptr, "bad arguments passed to %s (see above for details)",
426                  PrettyMethod(h_m.Get()).c_str());
427  }
428}
429
430static mirror::ArtMethod* FindVirtualMethod(mirror::Object* receiver,
431                                            mirror::ArtMethod* method)
432    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
433  return receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(method);
434}
435
436
437static void InvokeWithArgArray(const ScopedObjectAccessAlreadyRunnable& soa,
438                               mirror::ArtMethod* method, ArgArray* arg_array, JValue* result,
439                               const char* shorty)
440    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
441  uint32_t* args = arg_array->GetArray();
442  if (UNLIKELY(soa.Env()->check_jni)) {
443    CheckMethodArguments(soa.Vm(), method, args);
444  }
445  method->Invoke(soa.Self(), args, arg_array->GetNumBytes(), result, shorty);
446}
447
448JValue InvokeWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa, jobject obj, jmethodID mid,
449                         va_list args)
450    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
451  // We want to make sure that the stack is not within a small distance from the
452  // protected region in case we are calling into a leaf function whose stack
453  // check has been elided.
454  if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
455    ThrowStackOverflowError(soa.Self());
456    return JValue();
457  }
458
459  mirror::ArtMethod* method = soa.DecodeMethod(mid);
460  mirror::Object* receiver = method->IsStatic() ? nullptr : soa.Decode<mirror::Object*>(obj);
461  uint32_t shorty_len = 0;
462  const char* shorty = method->GetShorty(&shorty_len);
463  JValue result;
464  ArgArray arg_array(shorty, shorty_len);
465  arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
466  InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
467  return result;
468}
469
470JValue InvokeWithJValues(const ScopedObjectAccessAlreadyRunnable& soa, mirror::Object* receiver,
471                         jmethodID mid, jvalue* args) {
472  // We want to make sure that the stack is not within a small distance from the
473  // protected region in case we are calling into a leaf function whose stack
474  // check has been elided.
475  if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
476    ThrowStackOverflowError(soa.Self());
477    return JValue();
478  }
479
480  mirror::ArtMethod* method = soa.DecodeMethod(mid);
481  uint32_t shorty_len = 0;
482  const char* shorty = method->GetShorty(&shorty_len);
483  JValue result;
484  ArgArray arg_array(shorty, shorty_len);
485  arg_array.BuildArgArrayFromJValues(soa, receiver, args);
486  InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
487  return result;
488}
489
490JValue InvokeVirtualOrInterfaceWithJValues(const ScopedObjectAccessAlreadyRunnable& soa,
491                                           mirror::Object* receiver, jmethodID mid, jvalue* args) {
492  // We want to make sure that the stack is not within a small distance from the
493  // protected region in case we are calling into a leaf function whose stack
494  // check has been elided.
495  if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
496    ThrowStackOverflowError(soa.Self());
497    return JValue();
498  }
499
500  mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
501  uint32_t shorty_len = 0;
502  const char* shorty = method->GetShorty(&shorty_len);
503  JValue result;
504  ArgArray arg_array(shorty, shorty_len);
505  arg_array.BuildArgArrayFromJValues(soa, receiver, args);
506  InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
507  return result;
508}
509
510JValue InvokeVirtualOrInterfaceWithVarArgs(const ScopedObjectAccessAlreadyRunnable& soa,
511                                           jobject obj, jmethodID mid, va_list args) {
512  // We want to make sure that the stack is not within a small distance from the
513  // protected region in case we are calling into a leaf function whose stack
514  // check has been elided.
515  if (UNLIKELY(__builtin_frame_address(0) < soa.Self()->GetStackEnd())) {
516    ThrowStackOverflowError(soa.Self());
517    return JValue();
518  }
519
520  mirror::Object* receiver = soa.Decode<mirror::Object*>(obj);
521  mirror::ArtMethod* method = FindVirtualMethod(receiver, soa.DecodeMethod(mid));
522  uint32_t shorty_len = 0;
523  const char* shorty = method->GetShorty(&shorty_len);
524  JValue result;
525  ArgArray arg_array(shorty, shorty_len);
526  arg_array.BuildArgArrayFromVarArgs(soa, receiver, args);
527  InvokeWithArgArray(soa, method, &arg_array, &result, shorty);
528  return result;
529}
530
531void InvokeWithShadowFrame(Thread* self, ShadowFrame* shadow_frame, uint16_t arg_offset,
532                           MethodHelper* mh, JValue* result) {
533  // We want to make sure that the stack is not within a small distance from the
534  // protected region in case we are calling into a leaf function whose stack
535  // check has been elided.
536  if (UNLIKELY(__builtin_frame_address(0) < self->GetStackEnd())) {
537    ThrowStackOverflowError(self);
538    return;
539  }
540
541  ArgArray arg_array(mh->GetShorty(), mh->GetShortyLength());
542  arg_array.BuildArgArrayFromFrame(shadow_frame, arg_offset);
543  shadow_frame->GetMethod()->Invoke(self, arg_array.GetArray(), arg_array.GetNumBytes(), result,
544                                    mh->GetShorty());
545}
546
547jobject InvokeMethod(const ScopedObjectAccessAlreadyRunnable& soa, jobject javaMethod,
548                     jobject javaReceiver, jobject javaArgs, bool accessible) {
549  // We want to make sure that the stack is not within a small distance from the
550  // protected region in case we are calling into a leaf function whose stack
551  // check has been elided.
552  if (UNLIKELY(__builtin_frame_address(0) <
553               soa.Self()->GetStackEndForInterpreter(true))) {
554    ThrowStackOverflowError(soa.Self());
555    return nullptr;
556  }
557
558  mirror::ArtMethod* m = mirror::ArtMethod::FromReflectedMethod(soa, javaMethod);
559
560  mirror::Class* declaring_class = m->GetDeclaringClass();
561  if (UNLIKELY(!declaring_class->IsInitialized())) {
562    StackHandleScope<1> hs(soa.Self());
563    Handle<mirror::Class> h_class(hs.NewHandle(declaring_class));
564    if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(soa.Self(), h_class, true, true)) {
565      return nullptr;
566    }
567    declaring_class = h_class.Get();
568  }
569
570  mirror::Object* receiver = nullptr;
571  if (!m->IsStatic()) {
572    // Check that the receiver is non-null and an instance of the field's declaring class.
573    receiver = soa.Decode<mirror::Object*>(javaReceiver);
574    if (!VerifyObjectIsClass(receiver, declaring_class)) {
575      return NULL;
576    }
577
578    // Find the actual implementation of the virtual method.
579    m = receiver->GetClass()->FindVirtualMethodForVirtualOrInterface(m);
580  }
581
582  // Get our arrays of arguments and their types, and check they're the same size.
583  mirror::ObjectArray<mirror::Object>* objects =
584      soa.Decode<mirror::ObjectArray<mirror::Object>*>(javaArgs);
585  const DexFile::TypeList* classes = m->GetParameterTypeList();
586  uint32_t classes_size = (classes == nullptr) ? 0 : classes->Size();
587  uint32_t arg_count = (objects != nullptr) ? objects->GetLength() : 0;
588  if (arg_count != classes_size) {
589    ThrowIllegalArgumentException(NULL,
590                                  StringPrintf("Wrong number of arguments; expected %d, got %d",
591                                               classes_size, arg_count).c_str());
592    return NULL;
593  }
594
595  // If method is not set to be accessible, verify it can be accessed by the caller.
596  mirror::Class* calling_class = nullptr;
597  if (!accessible && !VerifyAccess(soa.Self(), receiver, declaring_class, m->GetAccessFlags(),
598                                   &calling_class)) {
599    ThrowIllegalAccessException(nullptr,
600        StringPrintf("Class %s cannot access %s method %s of class %s",
601            calling_class == nullptr ? "null" : PrettyClass(calling_class).c_str(),
602            PrettyJavaAccessFlags(m->GetAccessFlags()).c_str(),
603            PrettyMethod(m).c_str(),
604            m->GetDeclaringClass() == nullptr ? "null" :
605                PrettyClass(m->GetDeclaringClass()).c_str()).c_str());
606    return nullptr;
607  }
608
609  // Invoke the method.
610  JValue result;
611  uint32_t shorty_len = 0;
612  const char* shorty = m->GetShorty(&shorty_len);
613  ArgArray arg_array(shorty, shorty_len);
614  StackHandleScope<1> hs(soa.Self());
615  MethodHelper mh(hs.NewHandle(m));
616  if (!arg_array.BuildArgArrayFromObjectArray(soa, receiver, objects, mh)) {
617    CHECK(soa.Self()->IsExceptionPending());
618    return nullptr;
619  }
620
621  InvokeWithArgArray(soa, m, &arg_array, &result, shorty);
622
623  // Wrap any exception with "Ljava/lang/reflect/InvocationTargetException;" and return early.
624  if (soa.Self()->IsExceptionPending()) {
625    jthrowable th = soa.Env()->ExceptionOccurred();
626    soa.Env()->ExceptionClear();
627    jclass exception_class = soa.Env()->FindClass("java/lang/reflect/InvocationTargetException");
628    jmethodID mid = soa.Env()->GetMethodID(exception_class, "<init>", "(Ljava/lang/Throwable;)V");
629    jobject exception_instance = soa.Env()->NewObject(exception_class, mid, th);
630    soa.Env()->Throw(reinterpret_cast<jthrowable>(exception_instance));
631    return NULL;
632  }
633
634  // Box if necessary and return.
635  return soa.AddLocalReference<jobject>(BoxPrimitive(mh.GetReturnType()->GetPrimitiveType(),
636                                                     result));
637}
638
639bool VerifyObjectIsClass(mirror::Object* o, mirror::Class* c) {
640  if (o == NULL) {
641    ThrowNullPointerException(NULL, "null receiver");
642    return false;
643  } else if (!o->InstanceOf(c)) {
644    std::string expected_class_name(PrettyDescriptor(c));
645    std::string actual_class_name(PrettyTypeOf(o));
646    ThrowIllegalArgumentException(NULL,
647                                  StringPrintf("Expected receiver of type %s, but got %s",
648                                               expected_class_name.c_str(),
649                                               actual_class_name.c_str()).c_str());
650    return false;
651  }
652  return true;
653}
654
655mirror::Object* BoxPrimitive(Primitive::Type src_class, const JValue& value) {
656  if (src_class == Primitive::kPrimNot) {
657    return value.GetL();
658  }
659  if (src_class == Primitive::kPrimVoid) {
660    // There's no such thing as a void field, and void methods invoked via reflection return null.
661    return nullptr;
662  }
663
664  jmethodID m = nullptr;
665  const char* shorty;
666  switch (src_class) {
667  case Primitive::kPrimBoolean:
668    m = WellKnownClasses::java_lang_Boolean_valueOf;
669    shorty = "LZ";
670    break;
671  case Primitive::kPrimByte:
672    m = WellKnownClasses::java_lang_Byte_valueOf;
673    shorty = "LB";
674    break;
675  case Primitive::kPrimChar:
676    m = WellKnownClasses::java_lang_Character_valueOf;
677    shorty = "LC";
678    break;
679  case Primitive::kPrimDouble:
680    m = WellKnownClasses::java_lang_Double_valueOf;
681    shorty = "LD";
682    break;
683  case Primitive::kPrimFloat:
684    m = WellKnownClasses::java_lang_Float_valueOf;
685    shorty = "LF";
686    break;
687  case Primitive::kPrimInt:
688    m = WellKnownClasses::java_lang_Integer_valueOf;
689    shorty = "LI";
690    break;
691  case Primitive::kPrimLong:
692    m = WellKnownClasses::java_lang_Long_valueOf;
693    shorty = "LJ";
694    break;
695  case Primitive::kPrimShort:
696    m = WellKnownClasses::java_lang_Short_valueOf;
697    shorty = "LS";
698    break;
699  default:
700    LOG(FATAL) << static_cast<int>(src_class);
701    shorty = nullptr;
702  }
703
704  ScopedObjectAccessUnchecked soa(Thread::Current());
705  DCHECK_EQ(soa.Self()->GetState(), kRunnable);
706
707  ArgArray arg_array(shorty, 2);
708  JValue result;
709  if (src_class == Primitive::kPrimDouble || src_class == Primitive::kPrimLong) {
710    arg_array.AppendWide(value.GetJ());
711  } else {
712    arg_array.Append(value.GetI());
713  }
714
715  soa.DecodeMethod(m)->Invoke(soa.Self(), arg_array.GetArray(), arg_array.GetNumBytes(),
716                              &result, shorty);
717  return result.GetL();
718}
719
720static std::string UnboxingFailureKind(mirror::ArtField* f)
721    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
722  if (f != nullptr) {
723    return "field " + PrettyField(f, false);
724  }
725  return "result";
726}
727
728static bool UnboxPrimitive(const ThrowLocation* throw_location, mirror::Object* o,
729                           mirror::Class* dst_class, mirror::ArtField* f,
730                           JValue* unboxed_value)
731    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
732  bool unbox_for_result = (f == nullptr);
733  if (!dst_class->IsPrimitive()) {
734    if (UNLIKELY(o != nullptr && !o->InstanceOf(dst_class))) {
735      if (!unbox_for_result) {
736        ThrowIllegalArgumentException(throw_location,
737                                      StringPrintf("%s has type %s, got %s",
738                                                   UnboxingFailureKind(f).c_str(),
739                                                   PrettyDescriptor(dst_class).c_str(),
740                                                   PrettyTypeOf(o).c_str()).c_str());
741      } else {
742        ThrowClassCastException(throw_location,
743                                StringPrintf("Couldn't convert result of type %s to %s",
744                                             PrettyTypeOf(o).c_str(),
745                                             PrettyDescriptor(dst_class).c_str()).c_str());
746      }
747      return false;
748    }
749    unboxed_value->SetL(o);
750    return true;
751  }
752  if (UNLIKELY(dst_class->GetPrimitiveType() == Primitive::kPrimVoid)) {
753    ThrowIllegalArgumentException(throw_location,
754                                  StringPrintf("Can't unbox %s to void",
755                                               UnboxingFailureKind(f).c_str()).c_str());
756    return false;
757  }
758  if (UNLIKELY(o == nullptr)) {
759    if (!unbox_for_result) {
760      ThrowIllegalArgumentException(throw_location,
761                                    StringPrintf("%s has type %s, got null",
762                                                 UnboxingFailureKind(f).c_str(),
763                                                 PrettyDescriptor(dst_class).c_str()).c_str());
764    } else {
765      ThrowNullPointerException(throw_location,
766                                StringPrintf("Expected to unbox a '%s' primitive type but was returned null",
767                                             PrettyDescriptor(dst_class).c_str()).c_str());
768    }
769    return false;
770  }
771
772  JValue boxed_value;
773  mirror::Class* klass = o->GetClass();
774  mirror::Class* src_class = nullptr;
775  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
776  mirror::ArtField* primitive_field = o->GetClass()->GetIFields()->Get(0);
777  if (klass->DescriptorEquals("Ljava/lang/Boolean;")) {
778    src_class = class_linker->FindPrimitiveClass('Z');
779    boxed_value.SetZ(primitive_field->GetBoolean(o));
780  } else if (klass->DescriptorEquals("Ljava/lang/Byte;")) {
781    src_class = class_linker->FindPrimitiveClass('B');
782    boxed_value.SetB(primitive_field->GetByte(o));
783  } else if (klass->DescriptorEquals("Ljava/lang/Character;")) {
784    src_class = class_linker->FindPrimitiveClass('C');
785    boxed_value.SetC(primitive_field->GetChar(o));
786  } else if (klass->DescriptorEquals("Ljava/lang/Float;")) {
787    src_class = class_linker->FindPrimitiveClass('F');
788    boxed_value.SetF(primitive_field->GetFloat(o));
789  } else if (klass->DescriptorEquals("Ljava/lang/Double;")) {
790    src_class = class_linker->FindPrimitiveClass('D');
791    boxed_value.SetD(primitive_field->GetDouble(o));
792  } else if (klass->DescriptorEquals("Ljava/lang/Integer;")) {
793    src_class = class_linker->FindPrimitiveClass('I');
794    boxed_value.SetI(primitive_field->GetInt(o));
795  } else if (klass->DescriptorEquals("Ljava/lang/Long;")) {
796    src_class = class_linker->FindPrimitiveClass('J');
797    boxed_value.SetJ(primitive_field->GetLong(o));
798  } else if (klass->DescriptorEquals("Ljava/lang/Short;")) {
799    src_class = class_linker->FindPrimitiveClass('S');
800    boxed_value.SetS(primitive_field->GetShort(o));
801  } else {
802    std::string temp;
803    ThrowIllegalArgumentException(throw_location,
804        StringPrintf("%s has type %s, got %s", UnboxingFailureKind(f).c_str(),
805            PrettyDescriptor(dst_class).c_str(),
806            PrettyDescriptor(o->GetClass()->GetDescriptor(&temp)).c_str()).c_str());
807    return false;
808  }
809
810  return ConvertPrimitiveValue(throw_location, unbox_for_result,
811                               src_class->GetPrimitiveType(), dst_class->GetPrimitiveType(),
812                               boxed_value, unboxed_value);
813}
814
815bool UnboxPrimitiveForField(mirror::Object* o, mirror::Class* dst_class, mirror::ArtField* f,
816                            JValue* unboxed_value) {
817  DCHECK(f != nullptr);
818  return UnboxPrimitive(nullptr, o, dst_class, f, unboxed_value);
819}
820
821bool UnboxPrimitiveForResult(const ThrowLocation& throw_location, mirror::Object* o,
822                             mirror::Class* dst_class, JValue* unboxed_value) {
823  return UnboxPrimitive(&throw_location, o, dst_class, nullptr, unboxed_value);
824}
825
826bool VerifyAccess(Thread* self, mirror::Object* obj, mirror::Class* declaring_class,
827                  uint32_t access_flags, mirror::Class** calling_class) {
828  if ((access_flags & kAccPublic) != 0) {
829    return true;
830  }
831  NthCallerVisitor visitor(self, 2);
832  visitor.WalkStack();
833  if (UNLIKELY(visitor.caller == nullptr)) {
834    // The caller is an attached native thread.
835    return false;
836  }
837  mirror::Class* caller_class = visitor.caller->GetDeclaringClass();
838  if (caller_class == declaring_class) {
839    return true;
840  }
841  ScopedAssertNoThreadSuspension sants(self, "verify-access");
842  *calling_class = caller_class;
843  if ((access_flags & kAccPrivate) != 0) {
844    return false;
845  }
846  if ((access_flags & kAccProtected) != 0) {
847    if (obj != nullptr && !obj->InstanceOf(caller_class) &&
848        !declaring_class->IsInSamePackage(caller_class)) {
849      return false;
850    } else if (declaring_class->IsAssignableFrom(caller_class)) {
851      return true;
852    }
853  }
854  return declaring_class->IsInSamePackage(caller_class);
855}
856
857}  // namespace art
858