entrypoint_utils.cc revision 7655f29fabc0a12765de828914a18314382e5a35
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 177655f29fabc0a12765de828914a18314382e5a35Ian Rogers#include "entrypoints/entrypoint_utils.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 3341005ddb5576b8630a1084fbb3979ffa602c0599jeffhaonamespace art { 3441005ddb5576b8630a1084fbb3979ffa602c0599jeffhao 3557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers// Helper function to allocate array for FILLED_NEW_ARRAY. 3662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogersmirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* referrer, 372dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers int32_t component_count, Thread* self, 382dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers bool access_check) { 3957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(component_count < 0)) { 4062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowNegativeArraySizeException(component_count); 4157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return NULL; // Failure 42ea2a11d5f20814f17985ae3d4defc8dd843f19b9Ian Rogers } 4362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers mirror::Class* klass = referrer->GetDexCacheResolvedTypes()->Get(type_idx); 4457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve 4562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, referrer); 4657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (klass == NULL) { // Error 4750b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers DCHECK(self->IsExceptionPending()); 4857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return NULL; // Failure 49ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers } 50ea2a11d5f20814f17985ae3d4defc8dd843f19b9Ian Rogers } 5157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) { 5257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) { 5362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowRuntimeException("Bad filled array request for type %s", 5462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers PrettyDescriptor(klass).c_str()); 55573db4a2077380d81fa74ee2309162530db87a98Ian Rogers } else { 5662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location = self->GetCurrentLocationForThrow(); 5762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers DCHECK(throw_location.GetMethod() == referrer); 5862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers self->ThrowNewExceptionF(throw_location, "Ljava/lang/InternalError;", 5950b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers "Found type %s; filled-new-array not implemented for anything but \'int\'", 6050b35e2fd1a68cd1240e4a9d9f363e11764957d1Ian Rogers PrettyDescriptor(klass).c_str()); 61573db4a2077380d81fa74ee2309162530db87a98Ian Rogers } 6257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return NULL; // Failure 63ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers } else { 6457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (access_check) { 6562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers mirror::Class* referrer_klass = referrer->GetDeclaringClass(); 6662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers if (UNLIKELY(!referrer_klass->CanAccess(klass))) { 6762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowIllegalAccessErrorClass(referrer_klass, klass); 6857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return NULL; // Failure 6957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } 7057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } 7157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers DCHECK(klass->IsArrayClass()) << PrettyClass(klass); 722dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers return mirror::Array::Alloc(self, klass, component_count); 73ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers } 74ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers} 75ad25ac568407ceb14334e8551dd1c4dd0fd6993cIan Rogers 762dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::Field* FindFieldFromCode(uint32_t field_idx, const mirror::AbstractMethod* referrer, 77233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz Thread* self, FindFieldType type, size_t expected_size, 78233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz bool access_check) { 7908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers bool is_primitive; 8008f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers bool is_set; 8108f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers bool is_static; 8208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers switch (type) { 8308f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break; 8408f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break; 8508f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break; 8608f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break; 8708f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break; 8808f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break; 8908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break; 9008f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through. 9108f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers default: is_primitive = true; is_set = true; is_static = true; break; 9208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers } 9357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 942dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static); 9557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(resolved_field == NULL)) { 9608f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers DCHECK(self->IsExceptionPending()); // Throw exception and unwind. 9708f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers return NULL; // Failure. 98233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz } 99233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz mirror::Class* fields_class = resolved_field->GetDeclaringClass(); 100233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz if (access_check) { 101807a25640d4f4de8143b160b3bb8f552ffbf6f4aSebastien Hertz if (UNLIKELY(resolved_field->IsStatic() != is_static)) { 10287e552db94588455c081efd87dbde0cd96d02942Ian Rogers ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer); 10308f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers return NULL; 10408f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers } 1052dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Class* referring_class = referrer->GetDeclaringClass(); 106e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers if (UNLIKELY(!referring_class->CanAccess(fields_class) || 107e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers !referring_class->CanAccessMember(fields_class, 108e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers resolved_field->GetAccessFlags()))) { 109e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers // The referring class can't access the resolved field, this may occur as a result of a 110e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers // protected field being made public by a sub-class. Resort to the dex file to determine 111e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers // the correct class for the access check. 1124445a7e3398a6143939168097a3aa275b734504dIan Rogers const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile(); 113e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers fields_class = class_linker->ResolveType(dex_file, 114e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers dex_file.GetFieldId(field_idx).class_idx_, 115e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers referring_class); 116e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers if (UNLIKELY(!referring_class->CanAccess(fields_class))) { 11787e552db94588455c081efd87dbde0cd96d02942Ian Rogers ThrowIllegalAccessErrorClass(referring_class, fields_class); 118e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers return NULL; // failure 119e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class, 120e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers resolved_field->GetAccessFlags()))) { 12187e552db94588455c081efd87dbde0cd96d02942Ian Rogers ThrowIllegalAccessErrorField(referring_class, resolved_field); 122e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers return NULL; // failure 123e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers } 124e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers } 125e2645d3e2db211bfd75775a2185c135ff387161aIan Rogers if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) { 12687e552db94588455c081efd87dbde0cd96d02942Ian Rogers ThrowIllegalAccessErrorFinalField(referrer, resolved_field); 12757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return NULL; // failure 12857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 12957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers FieldHelper fh(resolved_field); 13057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || 13157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers fh.FieldSize() != expected_size)) { 13262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location = self->GetCurrentLocationForThrow(); 13362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers DCHECK(throw_location.GetMethod() == referrer); 13462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers self->ThrowNewExceptionF(throw_location, "Ljava/lang/NoSuchFieldError;", 13557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers "Attempted read of %zd-bit %s on field '%s'", 13657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers expected_size * (32 / sizeof(int32_t)), 13757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers is_primitive ? "primitive" : "non-primitive", 13857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers PrettyField(resolved_field, true).c_str()); 13957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return NULL; // failure 14060db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers } 14160db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers } 14260db5ab3a2e480db9236325a14cb5a867881d8bbIan Rogers } 143233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz if (!is_static) { 144233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz // instance fields must be being accessed on an initialized class 145233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz return resolved_field; 146233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz } else { 147233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz // If the class is initialized we're done. 148233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz if (fields_class->IsInitialized()) { 149233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz return resolved_field; 150233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) { 151233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz // Otherwise let's ensure the class is initialized before resolving the field. 152233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz return resolved_field; 153233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz } else { 154233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz DCHECK(self->IsExceptionPending()); // Throw exception and unwind 155233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz return NULL; // failure 156233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz } 157233ea8e084a95ad2a3af746dddbadb155db6a814Sebastien Hertz } 158ddbd01ac1660d57416879d5a576482f1048dde64Shih-wei Liao} 159ddbd01ac1660d57416879d5a576482f1048dde64Shih-wei Liao 16057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers// Slow path method resolution 1612dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogersmirror::AbstractMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object, 1622dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::AbstractMethod* referrer, 1632dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers Thread* self, bool access_check, InvokeType type) { 16457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 16557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers bool is_direct = type == kStatic || type == kDirect; 1662dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::AbstractMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type); 16757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(resolved_method == NULL)) { 16808f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers DCHECK(self->IsExceptionPending()); // Throw exception and unwind. 16908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers return NULL; // Failure. 170262e251e56d66bd309c6165964ac0a0be24447fejeffhao } else if (UNLIKELY(this_object == NULL && type != kStatic)) { 171262e251e56d66bd309c6165964ac0a0be24447fejeffhao // Maintain interpreter-like semantics where NullPointerException is thrown 172262e251e56d66bd309c6165964ac0a0be24447fejeffhao // after potential NoSuchMethodError from class linker. 17362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location = self->GetCurrentLocationForThrow(); 17462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers DCHECK(referrer == throw_location.GetMethod()); 17562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowNullPointerExceptionForMethodAccess(throw_location, method_idx, type); 176262e251e56d66bd309c6165964ac0a0be24447fejeffhao return NULL; // Failure. 177466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers } else { 17857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (!access_check) { 17957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (is_direct) { 18057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return resolved_method; 18157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else if (type == kInterface) { 1822dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::AbstractMethod* interface_method = 18357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); 18457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(interface_method == NULL)) { 18587e552db94588455c081efd87dbde0cd96d02942Ian Rogers ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object, 18687e552db94588455c081efd87dbde0cd96d02942Ian Rogers referrer); 18708f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers return NULL; // Failure. 18857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 18957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return interface_method; 19057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } 19157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 1922dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::ObjectArray<mirror::AbstractMethod>* vtable; 19357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers uint16_t vtable_index = resolved_method->GetMethodIndex(); 19457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (type == kSuper) { 19557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable(); 19657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 19757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers vtable = this_object->GetClass()->GetVTable(); 19857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } 19957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers // TODO: eliminate bounds check? 20057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return vtable->Get(vtable_index); 201051c9fc1de07bbc265af38cf60f061394163c68fElliott Hughes } 202466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers } else { 20308f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers // Incompatible class change should have been handled in resolve method. 20408f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) { 2052fc1427ee9c534ed44d72184ad6d74ea65f3d5b3Ian Rogers ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method, 2062fc1427ee9c534ed44d72184ad6d74ea65f3d5b3Ian Rogers referrer); 20708f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers return NULL; // Failure. 20808f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers } 2092dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Class* methods_class = resolved_method->GetDeclaringClass(); 2102dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Class* referring_class = referrer->GetDeclaringClass(); 21157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(!referring_class->CanAccess(methods_class) || 21257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers !referring_class->CanAccessMember(methods_class, 21357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers resolved_method->GetAccessFlags()))) { 21457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers // The referring class can't access the resolved method, this may occur as a result of a 21557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers // protected method being made public by implementing an interface that re-declares the 21657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers // method public. Resort to the dex file to determine the correct class for the access check 2174445a7e3398a6143939168097a3aa275b734504dIan Rogers const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile(); 21857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers methods_class = class_linker->ResolveType(dex_file, 21957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers dex_file.GetMethodId(method_idx).class_idx_, 22057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers referring_class); 22157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(!referring_class->CanAccess(methods_class))) { 22287e552db94588455c081efd87dbde0cd96d02942Ian Rogers ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class, 22387e552db94588455c081efd87dbde0cd96d02942Ian Rogers referrer, resolved_method, type); 22408f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers return NULL; // Failure. 22557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class, 22657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers resolved_method->GetAccessFlags()))) { 22787e552db94588455c081efd87dbde0cd96d02942Ian Rogers ThrowIllegalAccessErrorMethod(referring_class, resolved_method); 22808f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers return NULL; // Failure. 229c2b4447ae9c0c1e77595620acac6508999df6698Ian Rogers } 230c2b4447ae9c0c1e77595620acac6508999df6698Ian Rogers } 23157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (is_direct) { 23257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return resolved_method; 23357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else if (type == kInterface) { 2342dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::AbstractMethod* interface_method = 23557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); 23657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (UNLIKELY(interface_method == NULL)) { 23787e552db94588455c081efd87dbde0cd96d02942Ian Rogers ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object, 23887e552db94588455c081efd87dbde0cd96d02942Ian Rogers referrer); 23908f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers return NULL; // Failure. 24057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 24157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return interface_method; 24257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } 243466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers } else { 2442dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::ObjectArray<mirror::AbstractMethod>* vtable; 24557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers uint16_t vtable_index = resolved_method->GetMethodIndex(); 24657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (type == kSuper) { 2472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Class* super_class = referring_class->GetSuperClass(); 24857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (LIKELY(super_class != NULL)) { 24957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers vtable = referring_class->GetSuperClass()->GetVTable(); 25057b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 25157b86d47b66322693a070185fadfb43cb9c12eabIan Rogers vtable = NULL; 25257b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } 25357b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 25457b86d47b66322693a070185fadfb43cb9c12eabIan Rogers vtable = this_object->GetClass()->GetVTable(); 25557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } 25657b86d47b66322693a070185fadfb43cb9c12eabIan Rogers if (LIKELY(vtable != NULL && 25757b86d47b66322693a070185fadfb43cb9c12eabIan Rogers vtable_index < static_cast<uint32_t>(vtable->GetLength()))) { 25857b86d47b66322693a070185fadfb43cb9c12eabIan Rogers return vtable->GetWithoutChecks(vtable_index); 25957b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } else { 26008f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers // Behavior to agree with that of the verifier. 26108f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers MethodHelper mh(resolved_method); 26208f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(), 26362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers mh.GetSignature()); 26408f753d5859936f8d3524e9e4faa6cee353873eaIan Rogers return NULL; // Failure. 26557b86d47b66322693a070185fadfb43cb9c12eabIan Rogers } 266466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers } 267466bb25416b88fabd5d4387b7c7e5cc1ece78b8cIan Rogers } 268dfcdf1a0d2d8d75b4c701317e4a092498a8d1e9eIan Rogers } 269dfcdf1a0d2d8d75b4c701317e4a092498a8d1e9eIan Rogers} 270dfcdf1a0d2d8d75b4c701317e4a092498a8d1e9eIan Rogers 271d752132c73072084a3def9257cca4fcee76047b6jeffhaovoid ThrowStackOverflowError(Thread* self) { 272d752132c73072084a3def9257cca4fcee76047b6jeffhao CHECK(!self->IsHandlingStackOverflow()) << "Recursive stack overflow."; 27362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers 27462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers if (Runtime::Current()->GetInstrumentation()->AreExitStubsInstalled()) { 27562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers // Remove extra entry pushed onto second stack during method tracing. 27662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers Runtime::Current()->GetInstrumentation()->PopMethodForUnwind(self, false); 277d752132c73072084a3def9257cca4fcee76047b6jeffhao } 27862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers 279d752132c73072084a3def9257cca4fcee76047b6jeffhao self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute. 280d752132c73072084a3def9257cca4fcee76047b6jeffhao JNIEnvExt* env = self->GetJniEnv(); 281d752132c73072084a3def9257cca4fcee76047b6jeffhao std::string msg("stack size "); 282d752132c73072084a3def9257cca4fcee76047b6jeffhao msg += PrettySize(self->GetStackSize()); 283d752132c73072084a3def9257cca4fcee76047b6jeffhao // Use low-level JNI routine and pre-baked error class to avoid class linking operations that 284d752132c73072084a3def9257cca4fcee76047b6jeffhao // would consume more stack. 285d752132c73072084a3def9257cca4fcee76047b6jeffhao int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError, 286d752132c73072084a3def9257cca4fcee76047b6jeffhao msg.c_str(), NULL); 287d752132c73072084a3def9257cca4fcee76047b6jeffhao if (rc != JNI_OK) { 288d752132c73072084a3def9257cca4fcee76047b6jeffhao // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME 289d752132c73072084a3def9257cca4fcee76047b6jeffhao // or die in the CHECK below. We may want to throw a pre-baked StackOverflowError 290d752132c73072084a3def9257cca4fcee76047b6jeffhao // instead. 291d752132c73072084a3def9257cca4fcee76047b6jeffhao LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed."; 292d752132c73072084a3def9257cca4fcee76047b6jeffhao CHECK(self->IsExceptionPending()); 293d752132c73072084a3def9257cca4fcee76047b6jeffhao } 294d752132c73072084a3def9257cca4fcee76047b6jeffhao self->ResetDefaultStackEnd(); // Return to default stack size. 295d752132c73072084a3def9257cca4fcee76047b6jeffhao} 296d752132c73072084a3def9257cca4fcee76047b6jeffhao 297af6e67a4816d2593586115b89faa659225363246Ian RogersJValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty, 298af6e67a4816d2593586115b89faa659225363246Ian Rogers jobject rcvr_jobj, jobject interface_method_jobj, 299af6e67a4816d2593586115b89faa659225363246Ian Rogers std::vector<jvalue>& args) { 300af6e67a4816d2593586115b89faa659225363246Ian Rogers DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy)); 301af6e67a4816d2593586115b89faa659225363246Ian Rogers 302af6e67a4816d2593586115b89faa659225363246Ian Rogers // Build argument array possibly triggering GC. 303af6e67a4816d2593586115b89faa659225363246Ian Rogers soa.Self()->AssertThreadSuspensionIsAllowable(); 304af6e67a4816d2593586115b89faa659225363246Ian Rogers jobjectArray args_jobj = NULL; 305af6e67a4816d2593586115b89faa659225363246Ian Rogers const JValue zero; 306af6e67a4816d2593586115b89faa659225363246Ian Rogers if (args.size() > 0) { 307af6e67a4816d2593586115b89faa659225363246Ian Rogers args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL); 308af6e67a4816d2593586115b89faa659225363246Ian Rogers if (args_jobj == NULL) { 309af6e67a4816d2593586115b89faa659225363246Ian Rogers CHECK(soa.Self()->IsExceptionPending()); 310af6e67a4816d2593586115b89faa659225363246Ian Rogers return zero; 311af6e67a4816d2593586115b89faa659225363246Ian Rogers } 312af6e67a4816d2593586115b89faa659225363246Ian Rogers for (size_t i = 0; i < args.size(); ++i) { 313af6e67a4816d2593586115b89faa659225363246Ian Rogers if (shorty[i + 1] == 'L') { 314af6e67a4816d2593586115b89faa659225363246Ian Rogers jobject val = args.at(i).l; 315af6e67a4816d2593586115b89faa659225363246Ian Rogers soa.Env()->SetObjectArrayElement(args_jobj, i, val); 316af6e67a4816d2593586115b89faa659225363246Ian Rogers } else { 317af6e67a4816d2593586115b89faa659225363246Ian Rogers JValue jv; 318af6e67a4816d2593586115b89faa659225363246Ian Rogers jv.SetJ(args.at(i).j); 3192dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv); 320af6e67a4816d2593586115b89faa659225363246Ian Rogers if (val == NULL) { 321af6e67a4816d2593586115b89faa659225363246Ian Rogers CHECK(soa.Self()->IsExceptionPending()); 322af6e67a4816d2593586115b89faa659225363246Ian Rogers return zero; 323af6e67a4816d2593586115b89faa659225363246Ian Rogers } 3242dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set(i, val); 325af6e67a4816d2593586115b89faa659225363246Ian Rogers } 326af6e67a4816d2593586115b89faa659225363246Ian Rogers } 327af6e67a4816d2593586115b89faa659225363246Ian Rogers } 328af6e67a4816d2593586115b89faa659225363246Ian Rogers 329af6e67a4816d2593586115b89faa659225363246Ian Rogers // Call InvocationHandler.invoke(Object proxy, Method method, Object[] args). 330af6e67a4816d2593586115b89faa659225363246Ian Rogers jobject inv_hand = soa.Env()->GetObjectField(rcvr_jobj, 331af6e67a4816d2593586115b89faa659225363246Ian Rogers WellKnownClasses::java_lang_reflect_Proxy_h); 332af6e67a4816d2593586115b89faa659225363246Ian Rogers jvalue invocation_args[3]; 333af6e67a4816d2593586115b89faa659225363246Ian Rogers invocation_args[0].l = rcvr_jobj; 334af6e67a4816d2593586115b89faa659225363246Ian Rogers invocation_args[1].l = interface_method_jobj; 335af6e67a4816d2593586115b89faa659225363246Ian Rogers invocation_args[2].l = args_jobj; 336af6e67a4816d2593586115b89faa659225363246Ian Rogers jobject result = 337af6e67a4816d2593586115b89faa659225363246Ian Rogers soa.Env()->CallObjectMethodA(inv_hand, 338af6e67a4816d2593586115b89faa659225363246Ian Rogers WellKnownClasses::java_lang_reflect_InvocationHandler_invoke, 339af6e67a4816d2593586115b89faa659225363246Ian Rogers invocation_args); 340af6e67a4816d2593586115b89faa659225363246Ian Rogers 341af6e67a4816d2593586115b89faa659225363246Ian Rogers // Unbox result and handle error conditions. 34262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers if (LIKELY(!soa.Self()->IsExceptionPending())) { 34362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers if (shorty[0] == 'V' || (shorty[0] == 'L' && result == NULL)) { 344af6e67a4816d2593586115b89faa659225363246Ian Rogers // Do nothing. 345af6e67a4816d2593586115b89faa659225363246Ian Rogers return zero; 346af6e67a4816d2593586115b89faa659225363246Ian Rogers } else { 3472dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* result_ref = soa.Decode<mirror::Object*>(result); 34862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj); 34962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers mirror::AbstractMethod* interface_method = 35062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers soa.Decode<mirror::AbstractMethod*>(interface_method_jobj); 35162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers mirror::Class* result_type = MethodHelper(interface_method).GetReturnType(); 35262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers mirror::AbstractMethod* proxy_method; 35362d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers if (interface_method->GetDeclaringClass()->IsInterface()) { 35462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers proxy_method = rcvr->GetClass()->FindVirtualMethodForInterface(interface_method); 35562d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } else { 35662d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers // Proxy dispatch to a method defined in Object. 35762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers DCHECK(interface_method->GetDeclaringClass()->IsObjectClass()); 35862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers proxy_method = interface_method; 35962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers } 36062d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location(rcvr, proxy_method, -1); 36162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers JValue result_unboxed; 36262d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers if (!UnboxPrimitiveForResult(throw_location, result_ref, result_type, result_unboxed)) { 363530f71c040cb1a7b946d5566d5a746f08f2d082cIan Rogers DCHECK(soa.Self()->IsExceptionPending()); 36462d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers return zero; 365af6e67a4816d2593586115b89faa659225363246Ian Rogers } 366af6e67a4816d2593586115b89faa659225363246Ian Rogers return result_unboxed; 367af6e67a4816d2593586115b89faa659225363246Ian Rogers } 368af6e67a4816d2593586115b89faa659225363246Ian Rogers } else { 369af6e67a4816d2593586115b89faa659225363246Ian Rogers // In the case of checked exceptions that aren't declared, the exception must be wrapped by 370af6e67a4816d2593586115b89faa659225363246Ian Rogers // a UndeclaredThrowableException. 37162d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers mirror::Throwable* exception = soa.Self()->GetException(NULL); 372af6e67a4816d2593586115b89faa659225363246Ian Rogers if (exception->IsCheckedException()) { 3732dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj); 3742dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::SynthesizedProxyClass* proxy_class = 3752dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers down_cast<mirror::SynthesizedProxyClass*>(rcvr->GetClass()); 3762dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::AbstractMethod* interface_method = 3772dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers soa.Decode<mirror::AbstractMethod*>(interface_method_jobj); 3782dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::AbstractMethod* proxy_method = 379af6e67a4816d2593586115b89faa659225363246Ian Rogers rcvr->GetClass()->FindVirtualMethodForInterface(interface_method); 380af6e67a4816d2593586115b89faa659225363246Ian Rogers int throws_index = -1; 381af6e67a4816d2593586115b89faa659225363246Ian Rogers size_t num_virt_methods = proxy_class->NumVirtualMethods(); 382af6e67a4816d2593586115b89faa659225363246Ian Rogers for (size_t i = 0; i < num_virt_methods; i++) { 383af6e67a4816d2593586115b89faa659225363246Ian Rogers if (proxy_class->GetVirtualMethod(i) == proxy_method) { 384af6e67a4816d2593586115b89faa659225363246Ian Rogers throws_index = i; 385af6e67a4816d2593586115b89faa659225363246Ian Rogers break; 386af6e67a4816d2593586115b89faa659225363246Ian Rogers } 387af6e67a4816d2593586115b89faa659225363246Ian Rogers } 388af6e67a4816d2593586115b89faa659225363246Ian Rogers CHECK_NE(throws_index, -1); 3892dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index); 3902dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Class* exception_class = exception->GetClass(); 391af6e67a4816d2593586115b89faa659225363246Ian Rogers bool declares_exception = false; 392af6e67a4816d2593586115b89faa659225363246Ian Rogers for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) { 3932dd0e2cea360bc9206eb88ecc40d259e796c239dIan Rogers mirror::Class* declared_exception = declared_exceptions->Get(i); 394af6e67a4816d2593586115b89faa659225363246Ian Rogers declares_exception = declared_exception->IsAssignableFrom(exception_class); 395af6e67a4816d2593586115b89faa659225363246Ian Rogers } 396af6e67a4816d2593586115b89faa659225363246Ian Rogers if (!declares_exception) { 39762d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers ThrowLocation throw_location(rcvr, proxy_method, -1); 39862d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers soa.Self()->ThrowNewWrappedException(throw_location, 39962d6c772205b8859f0ebf7ad105402ec4c3e2e01Ian Rogers "Ljava/lang/reflect/UndeclaredThrowableException;", 400af6e67a4816d2593586115b89faa659225363246Ian Rogers NULL); 401af6e67a4816d2593586115b89faa659225363246Ian Rogers } 402af6e67a4816d2593586115b89faa659225363246Ian Rogers } 403af6e67a4816d2593586115b89faa659225363246Ian Rogers return zero; 404af6e67a4816d2593586115b89faa659225363246Ian Rogers } 405af6e67a4816d2593586115b89faa659225363246Ian Rogers} 406af6e67a4816d2593586115b89faa659225363246Ian Rogers 4072d831014d88e38c0c499ce8597dcdb17b9d4c4b9Shih-wei Liao} // namespace art 408