entrypoint_utils.cc revision 1d54e73444e017d3a65234e0f193846f3e27472b
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"
211d54e73444e017d3a65234e0f193846f3e27472bIan Rogers#include "gc/accounting/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,
145233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz                                 Thread* self, FindFieldType type, size_t expected_size,
146233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz                                 bool access_check) {
14708f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers  bool is_primitive;
14808f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers  bool is_set;
14908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers  bool is_static;
15008f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers  switch (type) {
15108f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case InstanceObjectRead:     is_primitive = false; is_set = false; is_static = false; break;
15208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case InstanceObjectWrite:    is_primitive = false; is_set = true;  is_static = false; break;
15308f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case InstancePrimitiveRead:  is_primitive = true;  is_set = false; is_static = false; break;
15408f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case InstancePrimitiveWrite: is_primitive = true;  is_set = true;  is_static = false; break;
15508f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case StaticObjectRead:       is_primitive = false; is_set = false; is_static = true;  break;
15608f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case StaticObjectWrite:      is_primitive = false; is_set = true;  is_static = true;  break;
15708f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case StaticPrimitiveRead:    is_primitive = true;  is_set = false; is_static = true;  break;
15808f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    case StaticPrimitiveWrite:   // Keep GCC happy by having a default handler, fall-through.
15908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    default:                     is_primitive = true;  is_set = true;  is_static = true;  break;
16008f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers  }
16157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1622dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static);
16357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  if (UNLIKELY(resolved_field == NULL)) {
16408f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    DCHECK(self->IsExceptionPending());  // Throw exception and unwind.
16508f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    return NULL;  // Failure.
166233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz  }
167233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz  mirror::Class* fields_class = resolved_field->GetDeclaringClass();
168233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz  if (access_check) {
169807a25640d4f4de8143b160b3bb8f552ffbf6f4aSebastien Hertz    if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
17087e552db94588455c081efd87dbde0cd96d02942Ian Rogers      ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer);
17108f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers      return NULL;
17208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    }
1732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers    mirror::Class* referring_class = referrer->GetDeclaringClass();
174e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers    if (UNLIKELY(!referring_class->CanAccess(fields_class) ||
175e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers                 !referring_class->CanAccessMember(fields_class,
176e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers                                                   resolved_field->GetAccessFlags()))) {
177e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      // The referring class can't access the resolved field, this may occur as a result of a
178e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      // protected field being made public by a sub-class. Resort to the dex file to determine
179e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      // the correct class for the access check.
1804445a7e3398a6143939168097a3aa275b734504dIan Rogers      const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
181e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      fields_class = class_linker->ResolveType(dex_file,
182e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers                                               dex_file.GetFieldId(field_idx).class_idx_,
183e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers                                               referring_class);
184e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      if (UNLIKELY(!referring_class->CanAccess(fields_class))) {
18587e552db94588455c081efd87dbde0cd96d02942Ian Rogers        ThrowIllegalAccessErrorClass(referring_class, fields_class);
186e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers        return NULL;  // failure
187e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class,
188e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers                                                            resolved_field->GetAccessFlags()))) {
18987e552db94588455c081efd87dbde0cd96d02942Ian Rogers        ThrowIllegalAccessErrorField(referring_class, resolved_field);
190e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers        return NULL;  // failure
191e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers      }
192e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers    }
193e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers    if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) {
19487e552db94588455c081efd87dbde0cd96d02942Ian Rogers      ThrowIllegalAccessErrorFinalField(referrer, resolved_field);
19557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      return NULL;  // failure
19657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    } else {
19757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      FieldHelper fh(resolved_field);
19857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      if (UNLIKELY(fh.IsPrimitiveType() != is_primitive ||
19957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                   fh.FieldSize() != expected_size)) {
20062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        ThrowLocation throw_location = self->GetCurrentLocationForThrow();
20162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        DCHECK(throw_location.GetMethod() == referrer);
20262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;",
20357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                 "Attempted read of %zd-bit %s on field '%s'",
20457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                 expected_size * (32 / sizeof(int32_t)),
20557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                 is_primitive ? "primitive" : "non-primitive",
20657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                 PrettyField(resolved_field, true).c_str());
20757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        return NULL;  // failure
20860db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers      }
20960db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers    }
21060db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers  }
211233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz  if (!is_static) {
212233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz    // instance fields must be being accessed on an initialized class
213233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz    return resolved_field;
214233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz  } else {
215233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz    // If the class is initialized we're done.
216233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz    if (fields_class->IsInitialized()) {
217233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz      return resolved_field;
218233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz    } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) {
219233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz      // Otherwise let's ensure the class is initialized before resolving the field.
220233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz      return resolved_field;
221233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz    } else {
222233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz      DCHECK(self->IsExceptionPending());  // Throw exception and unwind
223233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz      return NULL;  // failure
224233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz    }
225233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz  }
226ddbd01ac1660d57416879d5a576482f1048dde64Shih-wei Liao}
227ddbd01ac1660d57416879d5a576482f1048dde64Shih-wei Liao
22857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers// Slow path method resolution
2292dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::AbstractMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object,
2302dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                           mirror::AbstractMethod* referrer,
2312dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers                                           Thread* self, bool access_check, InvokeType type) {
23257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
23357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  bool is_direct = type == kStatic || type == kDirect;
2342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers  mirror::AbstractMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type);
23557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers  if (UNLIKELY(resolved_method == NULL)) {
23608f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    DCHECK(self->IsExceptionPending());  // Throw exception and unwind.
23708f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers    return NULL;  // Failure.
238262e251e56d66bd309c6165964ac0a0be24447fejeffhao  } else if (UNLIKELY(this_object == NULL && type != kStatic)) {
239262e251e56d66bd309c6165964ac0a0be24447fejeffhao    // Maintain interpreter-like semantics where NullPointerException is thrown
240262e251e56d66bd309c6165964ac0a0be24447fejeffhao    // after potential NoSuchMethodError from class linker.
24162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowLocation throw_location = self->GetCurrentLocationForThrow();
24262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    DCHECK(referrer == throw_location.GetMethod());
24362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type);
244262e251e56d66bd309c6165964ac0a0be24447fejeffhao    return NULL;  // Failure.
245466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers  } else {
24657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers    if (!access_check) {
24757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      if (is_direct) {
24857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        return resolved_method;
24957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      } else if (type == kInterface) {
2502dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::AbstractMethod* interface_method =
25157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers            this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
25257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (UNLIKELY(interface_method == NULL)) {
25387e552db94588455c081efd87dbde0cd96d02942Ian Rogers          ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
25487e552db94588455c081efd87dbde0cd96d02942Ian Rogers                                                                     referrer);
25508f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          return NULL;  // Failure.
25657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else {
25757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          return interface_method;
25857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        }
25957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      } else {
2602dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::ObjectArray<mirror::AbstractMethod>* vtable;
26157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        uint16_t vtable_index = resolved_method->GetMethodIndex();
26257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (type == kSuper) {
26357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable();
26457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else {
26557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          vtable = this_object->GetClass()->GetVTable();
26657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        }
26757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        // TODO: eliminate bounds check?
26857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        return vtable->Get(vtable_index);
269051c9fc1de07bbc265af38cf60f061394163c68fElliott Hughes      }
270466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers    } else {
27108f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers      // Incompatible class change should have been handled in resolve method.
27208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers      if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) {
2732fc1427ee9c534ed44d72184ad6d74ea65f3d5b3Ian Rogers        ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method,
2742fc1427ee9c534ed44d72184ad6d74ea65f3d5b3Ian Rogers                                          referrer);
27508f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers        return NULL;  // Failure.
27608f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers      }
2772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* methods_class = resolved_method->GetDeclaringClass();
2782dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* referring_class = referrer->GetDeclaringClass();
27957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      if (UNLIKELY(!referring_class->CanAccess(methods_class) ||
28057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                   !referring_class->CanAccessMember(methods_class,
28157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                                     resolved_method->GetAccessFlags()))) {
28257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        // The referring class can't access the resolved method, this may occur as a result of a
28357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        // protected method being made public by implementing an interface that re-declares the
28457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        // method public. Resort to the dex file to determine the correct class for the access check
2854445a7e3398a6143939168097a3aa275b734504dIan Rogers        const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile();
28657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        methods_class = class_linker->ResolveType(dex_file,
28757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                                  dex_file.GetMethodId(method_idx).class_idx_,
28857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                                  referring_class);
28957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (UNLIKELY(!referring_class->CanAccess(methods_class))) {
29087e552db94588455c081efd87dbde0cd96d02942Ian Rogers          ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class,
29187e552db94588455c081efd87dbde0cd96d02942Ian Rogers                                                        referrer, resolved_method, type);
29208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          return NULL;  // Failure.
29357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class,
29457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                                                              resolved_method->GetAccessFlags()))) {
29587e552db94588455c081efd87dbde0cd96d02942Ian Rogers          ThrowIllegalAccessErrorMethod(referring_class, resolved_method);
29608f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          return NULL;  // Failure.
297c2b4447ae9c0c1e77595620acac6508999df6698Ian Rogers        }
298c2b4447ae9c0c1e77595620acac6508999df6698Ian Rogers      }
29957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      if (is_direct) {
30057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        return resolved_method;
30157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers      } else if (type == kInterface) {
3022dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::AbstractMethod* interface_method =
30357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers            this_object->GetClass()->FindVirtualMethodForInterface(resolved_method);
30457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (UNLIKELY(interface_method == NULL)) {
30587e552db94588455c081efd87dbde0cd96d02942Ian Rogers          ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object,
30687e552db94588455c081efd87dbde0cd96d02942Ian Rogers                                                                     referrer);
30708f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          return NULL;  // Failure.
30857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else {
30957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          return interface_method;
31057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        }
311466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers      } else {
3122dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::ObjectArray<mirror::AbstractMethod>* vtable;
31357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        uint16_t vtable_index = resolved_method->GetMethodIndex();
31457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (type == kSuper) {
3152dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          mirror::Class* super_class = referring_class->GetSuperClass();
31657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          if (LIKELY(super_class != NULL)) {
31757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers            vtable = referring_class->GetSuperClass()->GetVTable();
31857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          } else {
31957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers            vtable = NULL;
32057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          }
32157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else {
32257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          vtable = this_object->GetClass()->GetVTable();
32357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        }
32457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        if (LIKELY(vtable != NULL &&
32557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers                   vtable_index < static_cast<uint32_t>(vtable->GetLength()))) {
32657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers          return vtable->GetWithoutChecks(vtable_index);
32757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        } else {
32808f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          // Behavior to agree with that of the verifier.
32908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          MethodHelper mh(resolved_method);
33008f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(),
33162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                 mh.GetSignature());
33208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers          return NULL;  // Failure.
33357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers        }
334466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers      }
335466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers    }
336dfcdf1a0d2d8d75b4c701317e4a092498a8d1e9eIan Rogers  }
337dfcdf1a0d2d8d75b4c701317e4a092498a8d1e9eIan Rogers}
338dfcdf1a0d2d8d75b4c701317e4a092498a8d1e9eIan Rogers
339d752132c73072084a3def9257cca4fcee76047b6jeffhaovoid ThrowStackOverflowError(Thread* self) {
340d752132c73072084a3def9257cca4fcee76047b6jeffhao  CHECK(!self->IsHandlingStackOverflow()) << "Recursive stack overflow.";
34162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
34262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) {
34362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    // Remove extra entry pushed onto second stack during method tracing.
34462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    Runtime::Current()->GetInstrumentation()->PopMethodForUnwind(self, false);
345d752132c73072084a3def9257cca4fcee76047b6jeffhao  }
34662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers
347d752132c73072084a3def9257cca4fcee76047b6jeffhao  self->SetStackEndForStackOverflow();  // Allow space on the stack for constructor to execute.
348d752132c73072084a3def9257cca4fcee76047b6jeffhao  JNIEnvExt* env = self->GetJniEnv();
349d752132c73072084a3def9257cca4fcee76047b6jeffhao  std::string msg("stack size ");
350d752132c73072084a3def9257cca4fcee76047b6jeffhao  msg += PrettySize(self->GetStackSize());
351d752132c73072084a3def9257cca4fcee76047b6jeffhao  // Use low-level JNI routine and pre-baked error class to avoid class linking operations that
352d752132c73072084a3def9257cca4fcee76047b6jeffhao  // would consume more stack.
353d752132c73072084a3def9257cca4fcee76047b6jeffhao  int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError,
354d752132c73072084a3def9257cca4fcee76047b6jeffhao                                    msg.c_str(), NULL);
355d752132c73072084a3def9257cca4fcee76047b6jeffhao  if (rc != JNI_OK) {
356d752132c73072084a3def9257cca4fcee76047b6jeffhao    // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME
357d752132c73072084a3def9257cca4fcee76047b6jeffhao    //       or die in the CHECK below. We may want to throw a pre-baked StackOverflowError
358d752132c73072084a3def9257cca4fcee76047b6jeffhao    //       instead.
359d752132c73072084a3def9257cca4fcee76047b6jeffhao    LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed.";
360d752132c73072084a3def9257cca4fcee76047b6jeffhao    CHECK(self->IsExceptionPending());
361d752132c73072084a3def9257cca4fcee76047b6jeffhao  }
362d752132c73072084a3def9257cca4fcee76047b6jeffhao  self->ResetDefaultStackEnd();  // Return to default stack size.
363d752132c73072084a3def9257cca4fcee76047b6jeffhao}
364d752132c73072084a3def9257cca4fcee76047b6jeffhao
365af6e67a4816d2593586115b89faa659225363246Ian RogersJValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty,
366af6e67a4816d2593586115b89faa659225363246Ian Rogers                                    jobject rcvr_jobj, jobject interface_method_jobj,
367af6e67a4816d2593586115b89faa659225363246Ian Rogers                                    std::vector<jvalue>& args) {
368af6e67a4816d2593586115b89faa659225363246Ian Rogers  DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy));
369af6e67a4816d2593586115b89faa659225363246Ian Rogers
370af6e67a4816d2593586115b89faa659225363246Ian Rogers  // Build argument array possibly triggering GC.
371af6e67a4816d2593586115b89faa659225363246Ian Rogers  soa.Self()->AssertThreadSuspensionIsAllowable();
372af6e67a4816d2593586115b89faa659225363246Ian Rogers  jobjectArray args_jobj = NULL;
373af6e67a4816d2593586115b89faa659225363246Ian Rogers  const JValue zero;
374af6e67a4816d2593586115b89faa659225363246Ian Rogers  if (args.size() > 0) {
375af6e67a4816d2593586115b89faa659225363246Ian Rogers    args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL);
376af6e67a4816d2593586115b89faa659225363246Ian Rogers    if (args_jobj == NULL) {
377af6e67a4816d2593586115b89faa659225363246Ian Rogers      CHECK(soa.Self()->IsExceptionPending());
378af6e67a4816d2593586115b89faa659225363246Ian Rogers      return zero;
379af6e67a4816d2593586115b89faa659225363246Ian Rogers    }
380af6e67a4816d2593586115b89faa659225363246Ian Rogers    for (size_t i = 0; i < args.size(); ++i) {
381af6e67a4816d2593586115b89faa659225363246Ian Rogers      if (shorty[i + 1] == 'L') {
382af6e67a4816d2593586115b89faa659225363246Ian Rogers        jobject val = args.at(i).l;
383af6e67a4816d2593586115b89faa659225363246Ian Rogers        soa.Env()->SetObjectArrayElement(args_jobj, i, val);
384af6e67a4816d2593586115b89faa659225363246Ian Rogers      } else {
385af6e67a4816d2593586115b89faa659225363246Ian Rogers        JValue jv;
386af6e67a4816d2593586115b89faa659225363246Ian Rogers        jv.SetJ(args.at(i).j);
3872dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv);
388af6e67a4816d2593586115b89faa659225363246Ian Rogers        if (val == NULL) {
389af6e67a4816d2593586115b89faa659225363246Ian Rogers          CHECK(soa.Self()->IsExceptionPending());
390af6e67a4816d2593586115b89faa659225363246Ian Rogers          return zero;
391af6e67a4816d2593586115b89faa659225363246Ian Rogers        }
3922dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set(i, val);
393af6e67a4816d2593586115b89faa659225363246Ian Rogers      }
394af6e67a4816d2593586115b89faa659225363246Ian Rogers    }
395af6e67a4816d2593586115b89faa659225363246Ian Rogers  }
396af6e67a4816d2593586115b89faa659225363246Ian Rogers
397af6e67a4816d2593586115b89faa659225363246Ian Rogers  // Call InvocationHandler.invoke(Object proxy, Method method, Object[] args).
398af6e67a4816d2593586115b89faa659225363246Ian Rogers  jobject inv_hand = soa.Env()->GetObjectField(rcvr_jobj,
399af6e67a4816d2593586115b89faa659225363246Ian Rogers                                               WellKnownClasses::java_lang_reflect_Proxy_h);
400af6e67a4816d2593586115b89faa659225363246Ian Rogers  jvalue invocation_args[3];
401af6e67a4816d2593586115b89faa659225363246Ian Rogers  invocation_args[0].l = rcvr_jobj;
402af6e67a4816d2593586115b89faa659225363246Ian Rogers  invocation_args[1].l = interface_method_jobj;
403af6e67a4816d2593586115b89faa659225363246Ian Rogers  invocation_args[2].l = args_jobj;
404af6e67a4816d2593586115b89faa659225363246Ian Rogers  jobject result =
405af6e67a4816d2593586115b89faa659225363246Ian Rogers      soa.Env()->CallObjectMethodA(inv_hand,
406af6e67a4816d2593586115b89faa659225363246Ian Rogers                                   WellKnownClasses::java_lang_reflect_InvocationHandler_invoke,
407af6e67a4816d2593586115b89faa659225363246Ian Rogers                                   invocation_args);
408af6e67a4816d2593586115b89faa659225363246Ian Rogers
409af6e67a4816d2593586115b89faa659225363246Ian Rogers  // Unbox result and handle error conditions.
41062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers  if (LIKELY(!soa.Self()->IsExceptionPending())) {
41162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    if (shorty[0] == 'V' || (shorty[0] == 'L' && result == NULL)) {
412af6e67a4816d2593586115b89faa659225363246Ian Rogers      // Do nothing.
413af6e67a4816d2593586115b89faa659225363246Ian Rogers      return zero;
414af6e67a4816d2593586115b89faa659225363246Ian Rogers    } else {
4152dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* result_ref = soa.Decode<mirror::Object*>(result);
41662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
41762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      mirror::AbstractMethod* interface_method =
41862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers          soa.Decode<mirror::AbstractMethod*>(interface_method_jobj);
41962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      mirror::Class* result_type = MethodHelper(interface_method).GetReturnType();
42062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      mirror::AbstractMethod* proxy_method;
42162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      if (interface_method->GetDeclaringClass()->IsInterface()) {
42262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
42362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      } else {
42462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        // Proxy dispatch to a method defined in Object.
42562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        DCHECK(interface_method->GetDeclaringClass()->IsObjectClass());
42662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        proxy_method = interface_method;
42762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      }
42862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      ThrowLocation throw_location(rcvr, proxy_method, -1);
42962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      JValue result_unboxed;
43062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers      if (!UnboxPrimitiveForResult(throw_location, result_ref, result_type, result_unboxed)) {
431530f71c040cb1a7b946d5566d5a746f08f2d082cIan Rogers        DCHECK(soa.Self()->IsExceptionPending());
43262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        return zero;
433af6e67a4816d2593586115b89faa659225363246Ian Rogers      }
434af6e67a4816d2593586115b89faa659225363246Ian Rogers      return result_unboxed;
435af6e67a4816d2593586115b89faa659225363246Ian Rogers    }
436af6e67a4816d2593586115b89faa659225363246Ian Rogers  } else {
437af6e67a4816d2593586115b89faa659225363246Ian Rogers    // In the case of checked exceptions that aren't declared, the exception must be wrapped by
438af6e67a4816d2593586115b89faa659225363246Ian Rogers    // a UndeclaredThrowableException.
43962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers    mirror::Throwable* exception = soa.Self()->GetException(NULL);
440af6e67a4816d2593586115b89faa659225363246Ian Rogers    if (exception->IsCheckedException()) {
4412dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj);
4422dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::SynthesizedProxyClass* proxy_class =
4432dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          down_cast<mirror::SynthesizedProxyClass*>(rcvr->GetClass());
4442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::AbstractMethod* interface_method =
4452dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers          soa.Decode<mirror::AbstractMethod*>(interface_method_jobj);
4462dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::AbstractMethod* proxy_method =
447af6e67a4816d2593586115b89faa659225363246Ian Rogers          rcvr->GetClass()->FindVirtualMethodForInterface(interface_method);
448af6e67a4816d2593586115b89faa659225363246Ian Rogers      int throws_index = -1;
449af6e67a4816d2593586115b89faa659225363246Ian Rogers      size_t num_virt_methods = proxy_class->NumVirtualMethods();
450af6e67a4816d2593586115b89faa659225363246Ian Rogers      for (size_t i = 0; i < num_virt_methods; i++) {
451af6e67a4816d2593586115b89faa659225363246Ian Rogers        if (proxy_class->GetVirtualMethod(i) == proxy_method) {
452af6e67a4816d2593586115b89faa659225363246Ian Rogers          throws_index = i;
453af6e67a4816d2593586115b89faa659225363246Ian Rogers          break;
454af6e67a4816d2593586115b89faa659225363246Ian Rogers        }
455af6e67a4816d2593586115b89faa659225363246Ian Rogers      }
456af6e67a4816d2593586115b89faa659225363246Ian Rogers      CHECK_NE(throws_index, -1);
4572dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index);
4582dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers      mirror::Class* exception_class = exception->GetClass();
459af6e67a4816d2593586115b89faa659225363246Ian Rogers      bool declares_exception = false;
460af6e67a4816d2593586115b89faa659225363246Ian Rogers      for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) {
4612dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers        mirror::Class* declared_exception = declared_exceptions->Get(i);
462af6e67a4816d2593586115b89faa659225363246Ian Rogers        declares_exception = declared_exception->IsAssignableFrom(exception_class);
463af6e67a4816d2593586115b89faa659225363246Ian Rogers      }
464af6e67a4816d2593586115b89faa659225363246Ian Rogers      if (!declares_exception) {
46562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        ThrowLocation throw_location(rcvr, proxy_method, -1);
46662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers        soa.Self()->ThrowNewWrappedException(throw_location,
46762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers                                             "Ljava/lang/reflect/UndeclaredThrowableException;",
468af6e67a4816d2593586115b89faa659225363246Ian Rogers                                             NULL);
469af6e67a4816d2593586115b89faa659225363246Ian Rogers      }
470af6e67a4816d2593586115b89faa659225363246Ian Rogers    }
471af6e67a4816d2593586115b89faa659225363246Ian Rogers    return zero;
472af6e67a4816d2593586115b89faa659225363246Ian Rogers  }
473af6e67a4816d2593586115b89faa659225363246Ian Rogers}
474af6e67a4816d2593586115b89faa659225363246Ian Rogers
4752d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao}  // namespace art
476