Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 2 | * Copyright (C) 2012 The Android Open Source Project |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "runtime_support.h" |
| 18 | |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 19 | #include "ScopedLocalRef.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 20 | #include "well_known_classes.h" |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 21 | |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 22 | double art_l2d(int64_t l) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 23 | return static_cast<double>(l); |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | float art_l2f(int64_t l) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 27 | return static_cast<float>(l); |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 28 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 29 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 30 | /* |
| 31 | * Float/double conversion requires clamping to min and max of integer form. If |
| 32 | * target doesn't support this normally, use these. |
| 33 | */ |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 34 | int64_t art_d2l(double d) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 35 | static const double kMaxLong = static_cast<double>(static_cast<int64_t>(0x7fffffffffffffffULL)); |
| 36 | static const double kMinLong = static_cast<double>(static_cast<int64_t>(0x8000000000000000ULL)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 37 | if (d >= kMaxLong) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 38 | return static_cast<int64_t>(0x7fffffffffffffffULL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 39 | } else if (d <= kMinLong) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 40 | return static_cast<int64_t>(0x8000000000000000ULL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 41 | } else if (d != d) { // NaN case |
| 42 | return 0; |
| 43 | } else { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 44 | return static_cast<int64_t>(d); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 48 | int64_t art_f2l(float f) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 49 | static const float kMaxLong = static_cast<float>(static_cast<int64_t>(0x7fffffffffffffffULL)); |
| 50 | static const float kMinLong = static_cast<float>(static_cast<int64_t>(0x8000000000000000ULL)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 51 | if (f >= kMaxLong) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 52 | return static_cast<int64_t>(0x7fffffffffffffffULL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 53 | } else if (f <= kMinLong) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 54 | return static_cast<int64_t>(0x8000000000000000ULL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 55 | } else if (f != f) { // NaN case |
| 56 | return 0; |
| 57 | } else { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 58 | return static_cast<int64_t>(f); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 62 | int32_t art_d2i(double d) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 63 | static const double kMaxInt = static_cast<double>(static_cast<int32_t>(0x7fffffffUL)); |
| 64 | static const double kMinInt = static_cast<double>(static_cast<int32_t>(0x80000000UL)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 65 | if (d >= kMaxInt) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 66 | return static_cast<int32_t>(0x7fffffffUL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 67 | } else if (d <= kMinInt) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 68 | return static_cast<int32_t>(0x80000000UL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 69 | } else if (d != d) { // NaN case |
| 70 | return 0; |
| 71 | } else { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 72 | return static_cast<int32_t>(d); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 76 | int32_t art_f2i(float f) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 77 | static const float kMaxInt = static_cast<float>(static_cast<int32_t>(0x7fffffffUL)); |
| 78 | static const float kMinInt = static_cast<float>(static_cast<int32_t>(0x80000000UL)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 79 | if (f >= kMaxInt) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 80 | return static_cast<int32_t>(0x7fffffffUL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 81 | } else if (f <= kMinInt) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 82 | return static_cast<int32_t>(0x80000000UL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 83 | } else if (f != f) { // NaN case |
| 84 | return 0; |
| 85 | } else { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 86 | return static_cast<int32_t>(f); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 90 | namespace art { |
| 91 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 92 | // Helper function to allocate array for FILLED_NEW_ARRAY. |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 93 | Array* CheckAndAllocArrayFromCode(uint32_t type_idx, AbstractMethod* method, int32_t component_count, |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 94 | Thread* self, bool access_check) { |
| 95 | if (UNLIKELY(component_count < 0)) { |
| 96 | self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count); |
| 97 | return NULL; // Failure |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 98 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 99 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 100 | if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve |
| 101 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 102 | if (klass == NULL) { // Error |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 103 | DCHECK(self->IsExceptionPending()); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 104 | return NULL; // Failure |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 105 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 106 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 107 | if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) { |
| 108 | if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 109 | self->ThrowNewExceptionF("Ljava/lang/RuntimeException;", |
| 110 | "Bad filled array request for type %s", |
| 111 | PrettyDescriptor(klass).c_str()); |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 112 | } else { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 113 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 114 | "Found type %s; filled-new-array not implemented for anything but \'int\'", |
| 115 | PrettyDescriptor(klass).c_str()); |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 116 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 117 | return NULL; // Failure |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 118 | } else { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 119 | if (access_check) { |
| 120 | Class* referrer = method->GetDeclaringClass(); |
| 121 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 122 | ThrowIllegalAccessErrorClass(referrer, klass); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 123 | return NULL; // Failure |
| 124 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 125 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 126 | DCHECK(klass->IsArrayClass()) << PrettyClass(klass); |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 127 | return Array::Alloc(self, klass, component_count); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
| 130 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 131 | Field* FindFieldFromCode(uint32_t field_idx, const AbstractMethod* referrer, Thread* self, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 132 | FindFieldType type, size_t expected_size) { |
| 133 | bool is_primitive; |
| 134 | bool is_set; |
| 135 | bool is_static; |
| 136 | switch (type) { |
| 137 | case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break; |
| 138 | case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break; |
| 139 | case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break; |
| 140 | case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break; |
| 141 | case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break; |
| 142 | case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break; |
| 143 | case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break; |
| 144 | case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through. |
| 145 | default: is_primitive = true; is_set = true; is_static = true; break; |
| 146 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 147 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 148 | Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static); |
| 149 | if (UNLIKELY(resolved_field == NULL)) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 150 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 151 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 152 | } else { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 153 | if (resolved_field->IsStatic() != is_static) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 154 | ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 155 | return NULL; |
| 156 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 157 | Class* fields_class = resolved_field->GetDeclaringClass(); |
| 158 | Class* referring_class = referrer->GetDeclaringClass(); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 159 | if (UNLIKELY(!referring_class->CanAccess(fields_class) || |
| 160 | !referring_class->CanAccessMember(fields_class, |
| 161 | resolved_field->GetAccessFlags()))) { |
| 162 | // The referring class can't access the resolved field, this may occur as a result of a |
| 163 | // protected field being made public by a sub-class. Resort to the dex file to determine |
| 164 | // the correct class for the access check. |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 165 | const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile(); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 166 | fields_class = class_linker->ResolveType(dex_file, |
| 167 | dex_file.GetFieldId(field_idx).class_idx_, |
| 168 | referring_class); |
| 169 | if (UNLIKELY(!referring_class->CanAccess(fields_class))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 170 | ThrowIllegalAccessErrorClass(referring_class, fields_class); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 171 | return NULL; // failure |
| 172 | } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class, |
| 173 | resolved_field->GetAccessFlags()))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 174 | ThrowIllegalAccessErrorField(referring_class, resolved_field); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 175 | return NULL; // failure |
| 176 | } |
| 177 | } |
| 178 | if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 179 | ThrowIllegalAccessErrorFinalField(referrer, resolved_field); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 180 | return NULL; // failure |
| 181 | } else { |
| 182 | FieldHelper fh(resolved_field); |
| 183 | if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || |
| 184 | fh.FieldSize() != expected_size)) { |
| 185 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 186 | "Attempted read of %zd-bit %s on field '%s'", |
| 187 | expected_size * (32 / sizeof(int32_t)), |
| 188 | is_primitive ? "primitive" : "non-primitive", |
| 189 | PrettyField(resolved_field, true).c_str()); |
| 190 | return NULL; // failure |
| 191 | } else if (!is_static) { |
| 192 | // instance fields must be being accessed on an initialized class |
| 193 | return resolved_field; |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 194 | } else { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 195 | // If the class is already initializing, we must be inside <clinit>, or |
| 196 | // we'd still be waiting for the lock. |
| 197 | if (fields_class->IsInitializing()) { |
| 198 | return resolved_field; |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 199 | } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 200 | return resolved_field; |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 201 | } else { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 202 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind |
| 203 | return NULL; // failure |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 204 | } |
| 205 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // Slow path method resolution |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 211 | AbstractMethod* FindMethodFromCode(uint32_t method_idx, Object* this_object, const AbstractMethod* referrer, |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 212 | Thread* self, bool access_check, InvokeType type) { |
| 213 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 214 | bool is_direct = type == kStatic || type == kDirect; |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 215 | AbstractMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 216 | if (UNLIKELY(resolved_method == NULL)) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 217 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 218 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 219 | } else { |
| 220 | if (!access_check) { |
| 221 | if (is_direct) { |
| 222 | return resolved_method; |
| 223 | } else if (type == kInterface) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 224 | AbstractMethod* interface_method = |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 225 | this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); |
| 226 | if (UNLIKELY(interface_method == NULL)) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 227 | ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object, |
| 228 | referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 229 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 230 | } else { |
| 231 | return interface_method; |
| 232 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 233 | } else { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 234 | ObjectArray<AbstractMethod>* vtable; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 235 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
| 236 | if (type == kSuper) { |
| 237 | vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable(); |
| 238 | } else { |
| 239 | vtable = this_object->GetClass()->GetVTable(); |
| 240 | } |
| 241 | // TODO: eliminate bounds check? |
| 242 | return vtable->Get(vtable_index); |
| 243 | } |
| 244 | } else { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 245 | // Incompatible class change should have been handled in resolve method. |
| 246 | if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 247 | ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method, |
| 248 | referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 249 | return NULL; // Failure. |
| 250 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 251 | Class* methods_class = resolved_method->GetDeclaringClass(); |
| 252 | Class* referring_class = referrer->GetDeclaringClass(); |
| 253 | if (UNLIKELY(!referring_class->CanAccess(methods_class) || |
| 254 | !referring_class->CanAccessMember(methods_class, |
| 255 | resolved_method->GetAccessFlags()))) { |
| 256 | // The referring class can't access the resolved method, this may occur as a result of a |
| 257 | // protected method being made public by implementing an interface that re-declares the |
| 258 | // method public. Resort to the dex file to determine the correct class for the access check |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 259 | const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 260 | methods_class = class_linker->ResolveType(dex_file, |
| 261 | dex_file.GetMethodId(method_idx).class_idx_, |
| 262 | referring_class); |
| 263 | if (UNLIKELY(!referring_class->CanAccess(methods_class))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 264 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class, |
| 265 | referrer, resolved_method, type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 266 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 267 | } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class, |
| 268 | resolved_method->GetAccessFlags()))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 269 | ThrowIllegalAccessErrorMethod(referring_class, resolved_method); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 270 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | if (is_direct) { |
| 274 | return resolved_method; |
| 275 | } else if (type == kInterface) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 276 | AbstractMethod* interface_method = |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 277 | this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); |
| 278 | if (UNLIKELY(interface_method == NULL)) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 279 | ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object, |
| 280 | referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 281 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 282 | } else { |
| 283 | return interface_method; |
| 284 | } |
| 285 | } else { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 286 | ObjectArray<AbstractMethod>* vtable; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 287 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
| 288 | if (type == kSuper) { |
| 289 | Class* super_class = referring_class->GetSuperClass(); |
| 290 | if (LIKELY(super_class != NULL)) { |
| 291 | vtable = referring_class->GetSuperClass()->GetVTable(); |
| 292 | } else { |
| 293 | vtable = NULL; |
| 294 | } |
| 295 | } else { |
| 296 | vtable = this_object->GetClass()->GetVTable(); |
| 297 | } |
| 298 | if (LIKELY(vtable != NULL && |
| 299 | vtable_index < static_cast<uint32_t>(vtable->GetLength()))) { |
| 300 | return vtable->GetWithoutChecks(vtable_index); |
| 301 | } else { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 302 | // Behavior to agree with that of the verifier. |
| 303 | MethodHelper mh(resolved_method); |
| 304 | ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(), |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 305 | mh.GetSignature(), referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 306 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 307 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 313 | Class* ResolveVerifyAndClinit(uint32_t type_idx, const AbstractMethod* referrer, Thread* self, |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 314 | bool can_run_clinit, bool verify_access) { |
| 315 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 316 | Class* klass = class_linker->ResolveType(type_idx, referrer); |
| 317 | if (UNLIKELY(klass == NULL)) { |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 318 | CHECK(self->IsExceptionPending()); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 319 | return NULL; // Failure - Indicate to caller to deliver exception |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 320 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 321 | // Perform access check if necessary. |
| 322 | Class* referring_class = referrer->GetDeclaringClass(); |
| 323 | if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 324 | ThrowIllegalAccessErrorClass(referring_class, klass); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 325 | return NULL; // Failure - Indicate to caller to deliver exception |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 326 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 327 | // If we're just implementing const-class, we shouldn't call <clinit>. |
| 328 | if (!can_run_clinit) { |
| 329 | return klass; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 330 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 331 | // If we are the <clinit> of this class, just return our storage. |
| 332 | // |
| 333 | // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished |
| 334 | // running. |
| 335 | if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) { |
| 336 | return klass; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 337 | } |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 338 | if (!class_linker->EnsureInitialized(klass, true, true)) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 339 | CHECK(self->IsExceptionPending()); |
| 340 | return NULL; // Failure - Indicate to caller to deliver exception |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 341 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 342 | referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass); |
| 343 | return klass; |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 346 | } // namespace art |