entrypoint_utils.cc revision 807a25640d4f4de8143b160b3bb8f552ffbf6f4a
12d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao/*
20f3c55331439970e01af67f80ac117c473bc04cfElliott Hughes * Copyright (C) 2012 The Android Open Source Project
32d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao *
42d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao * Licensed under the Apache License, Version 2.0 (the "License");
52d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao * you may not use this file except in compliance with the License.
62d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao * You may obtain a copy of the License at
72d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao *
82d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao *      http://www.apache.org/licenses/LICENSE-2.0
92d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao *
102d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao * Unless required by applicable law or agreed to in writing, software
112d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao * distributed under the License is distributed on an "AS IS" BASIS,
122d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao * See the License for the specific language governing permissions and
142d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao * limitations under the License.
152d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao */
162d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao
172d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao#include "runtime_support.h"
182d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao
192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "class_linker-inl.h"
204f6ad8ab428038129b2d0d6c40b7fd625cca15e1Ian Rogers#include "dex_file-inl.h"
212dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "gc/card_table-inl.h"
222dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/abstract_method-inl.h"
232dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/class-inl.h"
242dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/field-inl.h"
252dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object-inl.h"
262dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/object_array-inl.h"
272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers#include "mirror/proxy.h"
28af6e67a4816d2593586115b89faa659225363246Ian Rogers#include "reflection.h"
29af6e67a4816d2593586115b89faa659225363246Ian Rogers#include "scoped_thread_state_change.h"
305bb8601175bbb9cd761c715f4ba04f84d65e913bTDYa#include "ScopedLocalRef.h"
31eac766769e3114a078c188ea26776a81f0edb3cfElliott Hughes#include "well_known_classes.h"
325bb8601175bbb9cd761c715f4ba04f84d65e913bTDYa
3341005ddb5576b8630a1084fbb3979ffa602c0599jeffhaodouble art_l2d(int64_t l) {
34748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  return static_cast<double>(l);
3541005ddb5576b8630a1084fbb3979ffa602c0599jeffhao}
3641005ddb5576b8630a1084fbb3979ffa602c0599jeffhao
3741005ddb5576b8630a1084fbb3979ffa602c0599jeffhaofloat art_l2f(int64_t l) {
38748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  return static_cast<float>(l);
3941005ddb5576b8630a1084fbb3979ffa602c0599jeffhao}
402d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao
41776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers/*
42776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * Float/double conversion requires clamping to min and max of integer form.  If
43776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers * target doesn't support this normally, use these.
44776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers */
4541005ddb5576b8630a1084fbb3979ffa602c0599jeffhaoint64_t art_d2l(double d) {
46748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  static const double kMaxLong = static_cast<double>(static_cast<int64_t>(0x7fffffffffffffffULL));
47748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  static const double kMinLong = static_cast<double>(static_cast<int64_t>(0x8000000000000000ULL));
48776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  if (d >= kMaxLong) {
49008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien    return static_cast<int64_t>(0x7fffffffffffffffULL);
50776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else if (d <= kMinLong) {
51008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien    return static_cast<int64_t>(0x8000000000000000ULL);
52776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else if (d != d)  { // NaN case
53776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return 0;
54776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else {
55748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    return static_cast<int64_t>(d);
56776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
57776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}
58776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
5941005ddb5576b8630a1084fbb3979ffa602c0599jeffhaoint64_t art_f2l(float f) {
60748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  static const float kMaxLong = static_cast<float>(static_cast<int64_t>(0x7fffffffffffffffULL));
61748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes  static const float kMinLong = static_cast<float>(static_cast<int64_t>(0x8000000000000000ULL));
62776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  if (f >= kMaxLong) {
63008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien    return static_cast<int64_t>(0x7fffffffffffffffULL);
64776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else if (f <= kMinLong) {
65008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien    return static_cast<int64_t>(0x8000000000000000ULL);
66776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else if (f != f) { // NaN case
67776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return 0;
68776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else {
69748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    return static_cast<int64_t>(f);
70776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
71776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}
72776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
7341005ddb5576b8630a1084fbb3979ffa602c0599jeffhaoint32_t art_d2i(double d) {
74008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien  static const double kMaxInt = static_cast<double>(static_cast<int32_t>(0x7fffffffUL));
75008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien  static const double kMinInt = static_cast<double>(static_cast<int32_t>(0x80000000UL));
76776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  if (d >= kMaxInt) {
77008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien    return static_cast<int32_t>(0x7fffffffUL);
78776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else if (d <= kMinInt) {
79008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien    return static_cast<int32_t>(0x80000000UL);
80776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else if (d != d)  { // NaN case
81776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return 0;
82776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else {
83748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    return static_cast<int32_t>(d);
84776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
85776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}
86776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
8741005ddb5576b8630a1084fbb3979ffa602c0599jeffhaoint32_t art_f2i(float f) {
88008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien  static const float kMaxInt = static_cast<float>(static_cast<int32_t>(0x7fffffffUL));
89008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien  static const float kMinInt = static_cast<float>(static_cast<int32_t>(0x80000000UL));
90776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  if (f >= kMaxInt) {
91008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien    return static_cast<int32_t>(0x7fffffffUL);
92776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else if (f <= kMinInt) {
93008fa5114f356861f74f086f0b0c50fe42ef2891Logan Chien    return static_cast<int32_t>(0x80000000UL);
94776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else if (f != f) { // NaN case
95776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers    return 0;
96776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  } else {
97748474146da0c6484fa3dca0a700f612d47550c3Elliott Hughes    return static_cast<int32_t>(f);
98776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers  }
99776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers}
100776ac1fa61237db645adb4370a4aab888530caf4Ian Rogers
10141005ddb5576b8630a1084fbb3979ffa602c0599jeffhaonamespace art {
10241005ddb5576b8630a1084fbb3979ffa602c0599jeffhao
10357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers// Helper function to allocate array for FILLED_NEW_ARRAY.
10462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersmirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* referrer,
1052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                          int32_t component_count, Thread* self,
1062dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                          bool access_check) {
10757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  if (UNLIKELY(component_count < 0)) {
10862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowNegativeArraySizeException(component_count);
10957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    return NULL;  // Failure
110ea2a11d5f20814f17985ae3d4defc8dd843f19b9Ian Rogers  }
11162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  mirror::Class* klass = referrer->GetDexCacheResolvedTypes()->Get(type_idx);
11257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  if (UNLIKELY(klass == NULL)) {  // Not in dex cache so try to resolve
11362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, referrer);
11457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    if (klass == NULL) {  // Error
11550b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers      DCHECK(self->IsExceptionPending());
11657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      return NULL;  // Failure
117ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers    }
118ea2a11d5f20814f17985ae3d4defc8dd843f19b9Ian Rogers  }
11957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) {
12057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) {
12162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      ThrowRuntimeException("Bad filled array request for type %s",
12262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                            PrettyDescriptor(klass).c_str());
123573db4a2077380d81fa74ee2309162530db87a98Ian Rogers    } else {
12462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      ThrowLocation throw_location = self->GetCurrentLocationForThrow();
12562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      DCHECK(throw_location.GetMethod() == referrer);
12662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      self->ThrowNewExceptionF(throw_location, "Ljava/lang/InternalError;",
12750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers                               "Found type %s; filled-new-array not implemented for anything but \'int\'",
12850b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers                               PrettyDescriptor(klass).c_str());
129573db4a2077380d81fa74ee2309162530db87a98Ian Rogers    }
13057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    return NULL;  // Failure
131ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers  } else {
13257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    if (access_check) {
13362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      mirror::Class* referrer_klass = referrer->GetDeclaringClass();
13462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      if (UNLIKELY(!referrer_klass->CanAccess(klass))) {
13562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        ThrowIllegalAccessErrorClass(referrer_klass, klass);
13657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        return NULL;  // Failure
13757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      }
13857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    }
13957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    DCHECK(klass->IsArrayClass()) << PrettyClass(klass);
1402dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    return mirror::Array::Alloc(self, klass, component_count);
141ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers  }
142ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers}
143ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers
1442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::Field* FindFieldFromCode(uint32_t field_idx, const mirror::AbstractMethod* referrer,
1452dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                 Thread* self, FindFieldType type, size_t expected_size) {
14608f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers  bool is_primitive;
14708f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers  bool is_set;
14808f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers  bool is_static;
14908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers  switch (type) {
15008f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case InstanceObjectRead:     is_primitive = false; is_set = false; is_static = false; break;
15108f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case InstanceObjectWrite:    is_primitive = false; is_set = true;  is_static = false; break;
15208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case InstancePrimitiveRead:  is_primitive = true;  is_set = false; is_static = false; break;
15308f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case InstancePrimitiveWrite: is_primitive = true;  is_set = true;  is_static = false; break;
15408f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case StaticObjectRead:       is_primitive = false; is_set = false; is_static = true;  break;
15508f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case StaticObjectWrite:      is_primitive = false; is_set = true;  is_static = true;  break;
15608f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case StaticPrimitiveRead:    is_primitive = true;  is_set = false; is_static = true;  break;
15708f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case StaticPrimitiveWrite:   // Keep GCC happy by having a default handler, fall-through.
15808f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    default:                     is_primitive = true;  is_set = true;  is_static = true;  break;
15908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers  }
16057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1612dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
16257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  if (UNLIKELY(resolved_field == NULL)) {
16308f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    DCHECK(self->IsExceptionPending());  // Throw exception and unwind.
16408f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    return NULL;  // Failure.
16557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  } else {
166807a25640d4f4de8143b160b3bb8f552ffbf6f4aSebastien Hertz    if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
16787e552db94588455c081efd87dbde0cd96d02942Ian Rogers      ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
16808f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers      return NULL;
16908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    }
1702dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* fields_class = resolved_field->GetDeclaringClass();
1712dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* referring_class = referrer->GetDeclaringClass();
172e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers    if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
173e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers                 !referring_class->CanAccessMember(fields_class,
174e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers                                                   resolved_field->GetAccessFlags()))) {
175e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      // The referring class can't access the resolved field, this may occur as a result of a
176e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      // protected field being made public by a sub-class. Resort to the dex file to determine
177e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      // the correct class for the access check.
1784445a7e3398a6143939168097a3aa275b734504dIan Rogers      const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
179e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      fields_class = class_linker->ResolveType(dex_file,
180e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers                                               dex_file.GetFieldId(field_idx).class_idx_,
181e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers                                               referring_class);
182e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
18387e552db94588455c081efd87dbde0cd96d02942Ian Rogers        ThrowIllegalAccessErrorClass(referring_class, fields_class);
184e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers        return NULL;  // failure
185e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
186e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers                                                            resolved_field->GetAccessFlags()))) {
18787e552db94588455c081efd87dbde0cd96d02942Ian Rogers        ThrowIllegalAccessErrorField(referring_class, resolved_field);
188e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers        return NULL;  // failure
189e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      }
190e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers    }
191e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers    if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
19287e552db94588455c081efd87dbde0cd96d02942Ian Rogers      ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
19357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      return NULL;  // failure
19457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    } else {
19557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      FieldHelper fh(resolved_field);
19657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
19757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                   fh.FieldSize() != expected_size)) {
19862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        ThrowLocation throw_location = self->GetCurrentLocationForThrow();
19962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        DCHECK(throw_location.GetMethod() == referrer);
20062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
20157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                 "Attempted read of %zd-bit %s on field '%s'",
20257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                 expected_size * (32 / sizeof(int32_t)),
20357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                 is_primitive ? "primitive" : "non-primitive",
20457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                 PrettyField(resolved_field, true).c_str());
20557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        return NULL;  // failure
20657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      } else if (!is_static) {
20757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        // instance fields must be being accessed on an initialized class
20857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        return resolved_field;
20960db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers      } else {
210ede1807e3481a6937416964bb7556364f2d428e1Anwar Ghuloum        // If the class is initialized we're done.
211ede1807e3481a6937416964bb7556364f2d428e1Anwar Ghuloum        if (fields_class->IsInitialized()) {
21257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          return resolved_field;
2130045a290e6b79a274250e3112880c04bde437d4aIan Rogers        } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) {
2148447d84d847d4562d7a7bce62768c27e7d20a9aaAnwar Ghuloum          // Otherwise let's ensure the class is initialized before resolving the field.
21557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          return resolved_field;
21660db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers        } else {
21757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          DCHECK(self->IsExceptionPending());  // Throw exception and unwind
21857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          return NULL;  // failure
21960db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers        }
22060db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers      }
22160db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers    }
22260db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers  }
223ddbd01ac1660d57416879d5a576482f1048dde64Shih-wei Liao}
224ddbd01ac1660d57416879d5a576482f1048dde64Shih-wei Liao
22557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers// Slow path method resolution
2262dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::AbstractMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
2272dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                           mirror::AbstractMethod* referrer,
2282dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                           Thread* self, bool access_check, InvokeType type) {
22957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
23057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  bool is_direct = type == kStatic || type == kDirect;
2312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::AbstractMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
23257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  if (UNLIKELY(resolved_method == NULL)) {
23308f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    DCHECK(self->IsExceptionPending());  // Throw exception and unwind.
23408f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    return NULL;  // Failure.
235262e251e56d66bd309c6165964ac0a0be24447fejeffhao  } else if (UNLIKELY(this_object == NULL && type != kStatic)) {
236262e251e56d66bd309c6165964ac0a0be24447fejeffhao    // Maintain interpreter-like semantics where NullPointerException is thrown
237262e251e56d66bd309c6165964ac0a0be24447fejeffhao    // after potential NoSuchMethodError from class linker.
23862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation throw_location = self->GetCurrentLocationForThrow();
23962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    DCHECK(referrer == throw_location.GetMethod());
24062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
241262e251e56d66bd309c6165964ac0a0be24447fejeffhao    return NULL;  // Failure.
242466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers  } else {
24357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    if (!access_check) {
24457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      if (is_direct) {
24557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        return resolved_method;
24657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      } else if (type == kInterface) {
2472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::AbstractMethod* interface_method =
24857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers            this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
24957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (UNLIKELY(interface_method == NULL)) {
25087e552db94588455c081efd87dbde0cd96d02942Ian Rogers          ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
25187e552db94588455c081efd87dbde0cd96d02942Ian Rogers                                                                     referrer);
25208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          return NULL;  // Failure.
25357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else {
25457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          return interface_method;
25557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        }
25657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      } else {
2572dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::ObjectArray<mirror::AbstractMethod>* vtable;
25857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        uint16_t vtable_index = resolved_method->GetMethodIndex();
25957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (type == kSuper) {
26057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
26157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else {
26257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          vtable = this_object->GetClass()->GetVTable();
26357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        }
26457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        // TODO: eliminate bounds check?
26557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        return vtable->Get(vtable_index);
266051c9fc1de07bbc265af38cf60f061394163c68fElliott Hughes      }
267466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers    } else {
26808f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers      // Incompatible class change should have been handled in resolve method.
26908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers      if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
2702fc1427ee9c534ed44d72184ad6d74ea65f3d5b3Ian Rogers        ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
2712fc1427ee9c534ed44d72184ad6d74ea65f3d5b3Ian Rogers                                          referrer);
27208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers        return NULL;  // Failure.
27308f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers      }
2742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* methods_class = resolved_method->GetDeclaringClass();
2752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* referring_class = referrer->GetDeclaringClass();
27657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
27757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                   !referring_class->CanAccessMember(methods_class,
27857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                                     resolved_method->GetAccessFlags()))) {
27957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        // The referring class can't access the resolved method, this may occur as a result of a
28057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        // protected method being made public by implementing an interface that re-declares the
28157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        // method public. Resort to the dex file to determine the correct class for the access check
2824445a7e3398a6143939168097a3aa275b734504dIan Rogers        const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
28357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        methods_class = class_linker->ResolveType(dex_file,
28457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                                  dex_file.GetMethodId(method_idx).class_idx_,
28557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                                  referring_class);
28657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
28787e552db94588455c081efd87dbde0cd96d02942Ian Rogers          ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class,
28887e552db94588455c081efd87dbde0cd96d02942Ian Rogers                                                        referrer, resolved_method, type);
28908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          return NULL;  // Failure.
29057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
29157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                                              resolved_method->GetAccessFlags()))) {
29287e552db94588455c081efd87dbde0cd96d02942Ian Rogers          ThrowIllegalAccessErrorMethod(referring_class, resolved_method);
29308f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          return NULL;  // Failure.
294c2b4447ae9c0c1e77595620acac6508999df6698Ian Rogers        }
295c2b4447ae9c0c1e77595620acac6508999df6698Ian Rogers      }
29657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      if (is_direct) {
29757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        return resolved_method;
29857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      } else if (type == kInterface) {
2992dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::AbstractMethod* interface_method =
30057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers            this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
30157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (UNLIKELY(interface_method == NULL)) {
30287e552db94588455c081efd87dbde0cd96d02942Ian Rogers          ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
30387e552db94588455c081efd87dbde0cd96d02942Ian Rogers                                                                     referrer);
30408f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          return NULL;  // Failure.
30557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else {
30657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          return interface_method;
30757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        }
308466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers      } else {
3092dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::ObjectArray<mirror::AbstractMethod>* vtable;
31057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        uint16_t vtable_index = resolved_method->GetMethodIndex();
31157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (type == kSuper) {
3122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Class* super_class = referring_class->GetSuperClass();
31357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          if (LIKELY(super_class != NULL)) {
31457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers            vtable = referring_class->GetSuperClass()->GetVTable();
31557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          } else {
31657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers            vtable = NULL;
31757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          }
31857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else {
31957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          vtable = this_object->GetClass()->GetVTable();
32057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        }
32157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (LIKELY(vtable != NULL &&
32257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                   vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
32357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          return vtable->GetWithoutChecks(vtable_index);
32457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else {
32508f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          // Behavior to agree with that of the verifier.
32608f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          MethodHelper mh(resolved_method);
32708f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
32862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                 mh.GetSignature());
32908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          return NULL;  // Failure.
33057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        }
331466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers      }
332466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers    }
333dfcdf1a0d2d8d75b4c701317e4a092498a8d1e9eIan Rogers  }
334dfcdf1a0d2d8d75b4c701317e4a092498a8d1e9eIan Rogers}
335dfcdf1a0d2d8d75b4c701317e4a092498a8d1e9eIan Rogers
3362dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, const mirror::AbstractMethod* referrer,
3372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                      Thread* self, bool can_run_clinit, bool verify_access) {
33857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
3392dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* klass = class_linker->ResolveType(type_idx, referrer);
34057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  if (UNLIKELY(klass == NULL)) {
34157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    CHECK(self->IsExceptionPending());
34257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    return NULL;  // Failure - Indicate to caller to deliver exception
34357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  }
34457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  // Perform access check if necessary.
3452dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Class* referring_class = referrer->GetDeclaringClass();
34657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) {
34787e552db94588455c081efd87dbde0cd96d02942Ian Rogers    ThrowIllegalAccessErrorClass(referring_class, klass);
34857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    return NULL;  // Failure - Indicate to caller to deliver exception
34957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  }
35057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  // If we're just implementing const-class, we shouldn't call <clinit>.
35157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  if (!can_run_clinit) {
35257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    return klass;
35357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  }
35457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  // If we are the <clinit> of this class, just return our storage.
35557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  //
35657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished
35757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  // running.
35857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) {
35957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    return klass;
36057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  }
3610045a290e6b79a274250e3112880c04bde437d4aIan Rogers  if (!class_linker->EnsureInitialized(klass, true, true)) {
36257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    CHECK(self->IsExceptionPending());
36357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    return NULL;  // Failure - Indicate to caller to deliver exception
364a433b2e8edf91c8124abd8eb5b3bd558a40fc987Ian Rogers  }
36557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass);
36657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  return klass;
3672d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao}
3682d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao
369d752132c73072084a3def9257cca4fcee76047b6jeffhaovoid ThrowStackOverflowError(Thread* self) {
370d752132c73072084a3def9257cca4fcee76047b6jeffhao  CHECK(!self->IsHandlingStackOverflow()) << "Recursive stack overflow.";
37162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
37262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) {
37362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    // Remove extra entry pushed onto second stack during method tracing.
37462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    Runtime::Current()->GetInstrumentation()->PopMethodForUnwind(self, false);
375d752132c73072084a3def9257cca4fcee76047b6jeffhao  }
37662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
377d752132c73072084a3def9257cca4fcee76047b6jeffhao  self->SetStackEndForStackOverflow();  // Allow space on the stack for constructor to execute.
378d752132c73072084a3def9257cca4fcee76047b6jeffhao  JNIEnvExt* env = self->GetJniEnv();
379d752132c73072084a3def9257cca4fcee76047b6jeffhao  std::string msg("stack size ");
380d752132c73072084a3def9257cca4fcee76047b6jeffhao  msg += PrettySize(self->GetStackSize());
381d752132c73072084a3def9257cca4fcee76047b6jeffhao  // Use low-level JNI routine and pre-baked error class to avoid class linking operations that
382d752132c73072084a3def9257cca4fcee76047b6jeffhao  // would consume more stack.
383d752132c73072084a3def9257cca4fcee76047b6jeffhao  int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError,
384d752132c73072084a3def9257cca4fcee76047b6jeffhao                                    msg.c_str(), NULL);
385d752132c73072084a3def9257cca4fcee76047b6jeffhao  if (rc != JNI_OK) {
386d752132c73072084a3def9257cca4fcee76047b6jeffhao    // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME
387d752132c73072084a3def9257cca4fcee76047b6jeffhao    //       or die in the CHECK below. We may want to throw a pre-baked StackOverflowError
388d752132c73072084a3def9257cca4fcee76047b6jeffhao    //       instead.
389d752132c73072084a3def9257cca4fcee76047b6jeffhao    LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed.";
390d752132c73072084a3def9257cca4fcee76047b6jeffhao    CHECK(self->IsExceptionPending());
391d752132c73072084a3def9257cca4fcee76047b6jeffhao  }
392d752132c73072084a3def9257cca4fcee76047b6jeffhao  self->ResetDefaultStackEnd();  // Return to default stack size.
393d752132c73072084a3def9257cca4fcee76047b6jeffhao}
394d752132c73072084a3def9257cca4fcee76047b6jeffhao
395af6e67a4816d2593586115b89faa659225363246Ian RogersJValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
396af6e67a4816d2593586115b89faa659225363246Ian Rogers                                    jobject rcvr_jobj, jobject interface_method_jobj,
397af6e67a4816d2593586115b89faa659225363246Ian Rogers                                    std::vector<jvalue>& args) {
398af6e67a4816d2593586115b89faa659225363246Ian Rogers  DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
399af6e67a4816d2593586115b89faa659225363246Ian Rogers
400af6e67a4816d2593586115b89faa659225363246Ian Rogers  // Build argument array possibly triggering GC.
401af6e67a4816d2593586115b89faa659225363246Ian Rogers  soa.Self()->AssertThreadSuspensionIsAllowable();
402af6e67a4816d2593586115b89faa659225363246Ian Rogers  jobjectArray args_jobj = NULL;
403af6e67a4816d2593586115b89faa659225363246Ian Rogers  const JValue zero;
404af6e67a4816d2593586115b89faa659225363246Ian Rogers  if (args.size() > 0) {
405af6e67a4816d2593586115b89faa659225363246Ian Rogers    args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL);
406af6e67a4816d2593586115b89faa659225363246Ian Rogers    if (args_jobj == NULL) {
407af6e67a4816d2593586115b89faa659225363246Ian Rogers      CHECK(soa.Self()->IsExceptionPending());
408af6e67a4816d2593586115b89faa659225363246Ian Rogers      return zero;
409af6e67a4816d2593586115b89faa659225363246Ian Rogers    }
410af6e67a4816d2593586115b89faa659225363246Ian Rogers    for (size_t i = 0; i < args.size(); ++i) {
411af6e67a4816d2593586115b89faa659225363246Ian Rogers      if (shorty[i + 1] == 'L') {
412af6e67a4816d2593586115b89faa659225363246Ian Rogers        jobject val = args.at(i).l;
413af6e67a4816d2593586115b89faa659225363246Ian Rogers        soa.Env()->SetObjectArrayElement(args_jobj, i, val);
414af6e67a4816d2593586115b89faa659225363246Ian Rogers      } else {
415af6e67a4816d2593586115b89faa659225363246Ian Rogers        JValue jv;
416af6e67a4816d2593586115b89faa659225363246Ian Rogers        jv.SetJ(args.at(i).j);
4172dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
418af6e67a4816d2593586115b89faa659225363246Ian Rogers        if (val == NULL) {
419af6e67a4816d2593586115b89faa659225363246Ian Rogers          CHECK(soa.Self()->IsExceptionPending());
420af6e67a4816d2593586115b89faa659225363246Ian Rogers          return zero;
421af6e67a4816d2593586115b89faa659225363246Ian Rogers        }
4222dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set(i, val);
423af6e67a4816d2593586115b89faa659225363246Ian Rogers      }
424af6e67a4816d2593586115b89faa659225363246Ian Rogers    }
425af6e67a4816d2593586115b89faa659225363246Ian Rogers  }
426af6e67a4816d2593586115b89faa659225363246Ian Rogers
427af6e67a4816d2593586115b89faa659225363246Ian Rogers  // Call InvocationHandler.invoke(Object proxy, Method method, Object[] args).
428af6e67a4816d2593586115b89faa659225363246Ian Rogers  jobject inv_hand = soa.Env()->GetObjectField(rcvr_jobj,
429af6e67a4816d2593586115b89faa659225363246Ian Rogers                                               WellKnownClasses::java_lang_reflect_Proxy_h);
430af6e67a4816d2593586115b89faa659225363246Ian Rogers  jvalue invocation_args[3];
431af6e67a4816d2593586115b89faa659225363246Ian Rogers  invocation_args[0].l = rcvr_jobj;
432af6e67a4816d2593586115b89faa659225363246Ian Rogers  invocation_args[1].l = interface_method_jobj;
433af6e67a4816d2593586115b89faa659225363246Ian Rogers  invocation_args[2].l = args_jobj;
434af6e67a4816d2593586115b89faa659225363246Ian Rogers  jobject result =
435af6e67a4816d2593586115b89faa659225363246Ian Rogers      soa.Env()->CallObjectMethodA(inv_hand,
436af6e67a4816d2593586115b89faa659225363246Ian Rogers                                   WellKnownClasses::java_lang_reflect_InvocationHandler_invoke,
437af6e67a4816d2593586115b89faa659225363246Ian Rogers                                   invocation_args);
438af6e67a4816d2593586115b89faa659225363246Ian Rogers
439af6e67a4816d2593586115b89faa659225363246Ian Rogers  // Unbox result and handle error conditions.
44062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (LIKELY(!soa.Self()->IsExceptionPending())) {
44162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (shorty[0] == 'V' || (shorty[0] == 'L' && result == NULL)) {
442af6e67a4816d2593586115b89faa659225363246Ian Rogers      // Do nothing.
443af6e67a4816d2593586115b89faa659225363246Ian Rogers      return zero;
444af6e67a4816d2593586115b89faa659225363246Ian Rogers    } else {
4452dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* result_ref = soa.Decode<mirror::Object*>(result);
44662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
44762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      mirror::AbstractMethod* interface_method =
44862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers          soa.Decode<mirror::AbstractMethod*>(interface_method_jobj);
44962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      mirror::Class* result_type = MethodHelper(interface_method).GetReturnType();
45062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      mirror::AbstractMethod* proxy_method;
45162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      if (interface_method->GetDeclaringClass()->IsInterface()) {
45262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
45362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      } else {
45462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        // Proxy dispatch to a method defined in Object.
45562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        DCHECK(interface_method->GetDeclaringClass()->IsObjectClass());
45662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        proxy_method = interface_method;
45762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      }
45862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      ThrowLocation throw_location(rcvr, proxy_method, -1);
45962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      JValue result_unboxed;
46062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      if (!UnboxPrimitiveForResult(throw_location, result_ref, result_type, result_unboxed)) {
461530f71c040cb1a7b946d5566d5a746f08f2d082cIan Rogers        DCHECK(soa.Self()->IsExceptionPending());
46262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        return zero;
463af6e67a4816d2593586115b89faa659225363246Ian Rogers      }
464af6e67a4816d2593586115b89faa659225363246Ian Rogers      return result_unboxed;
465af6e67a4816d2593586115b89faa659225363246Ian Rogers    }
466af6e67a4816d2593586115b89faa659225363246Ian Rogers  } else {
467af6e67a4816d2593586115b89faa659225363246Ian Rogers    // In the case of checked exceptions that aren't declared, the exception must be wrapped by
468af6e67a4816d2593586115b89faa659225363246Ian Rogers    // a UndeclaredThrowableException.
46962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    mirror::Throwable* exception = soa.Self()->GetException(NULL);
470af6e67a4816d2593586115b89faa659225363246Ian Rogers    if (exception->IsCheckedException()) {
4712dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
4722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::SynthesizedProxyClass* proxy_class =
4732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          down_cast<mirror::SynthesizedProxyClass*>(rcvr->GetClass());
4742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::AbstractMethod* interface_method =
4752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          soa.Decode<mirror::AbstractMethod*>(interface_method_jobj);
4762dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::AbstractMethod* proxy_method =
477af6e67a4816d2593586115b89faa659225363246Ian Rogers          rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
478af6e67a4816d2593586115b89faa659225363246Ian Rogers      int throws_index = -1;
479af6e67a4816d2593586115b89faa659225363246Ian Rogers      size_t num_virt_methods = proxy_class->NumVirtualMethods();
480af6e67a4816d2593586115b89faa659225363246Ian Rogers      for (size_t i = 0; i < num_virt_methods; i++) {
481af6e67a4816d2593586115b89faa659225363246Ian Rogers        if (proxy_class->GetVirtualMethod(i) == proxy_method) {
482af6e67a4816d2593586115b89faa659225363246Ian Rogers          throws_index = i;
483af6e67a4816d2593586115b89faa659225363246Ian Rogers          break;
484af6e67a4816d2593586115b89faa659225363246Ian Rogers        }
485af6e67a4816d2593586115b89faa659225363246Ian Rogers      }
486af6e67a4816d2593586115b89faa659225363246Ian Rogers      CHECK_NE(throws_index, -1);
4872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
4882dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* exception_class = exception->GetClass();
489af6e67a4816d2593586115b89faa659225363246Ian Rogers      bool declares_exception = false;
490af6e67a4816d2593586115b89faa659225363246Ian Rogers      for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
4912dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Class* declared_exception = declared_exceptions->Get(i);
492af6e67a4816d2593586115b89faa659225363246Ian Rogers        declares_exception = declared_exception->IsAssignableFrom(exception_class);
493af6e67a4816d2593586115b89faa659225363246Ian Rogers      }
494af6e67a4816d2593586115b89faa659225363246Ian Rogers      if (!declares_exception) {
49562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        ThrowLocation throw_location(rcvr, proxy_method, -1);
49662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        soa.Self()->ThrowNewWrappedException(throw_location,
49762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                             "Ljava/lang/reflect/UndeclaredThrowableException;",
498af6e67a4816d2593586115b89faa659225363246Ian Rogers                                             NULL);
499af6e67a4816d2593586115b89faa659225363246Ian Rogers      }
500af6e67a4816d2593586115b89faa659225363246Ian Rogers    }
501af6e67a4816d2593586115b89faa659225363246Ian Rogers    return zero;
502af6e67a4816d2593586115b89faa659225363246Ian Rogers  }
503af6e67a4816d2593586115b89faa659225363246Ian Rogers}
504af6e67a4816d2593586115b89faa659225363246Ian Rogers
5052d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao}  // namespace art
506