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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 20 | #include "dex_file-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "gc/card_table-inl.h" |
| 22 | #include "mirror/abstract_method-inl.h" |
| 23 | #include "mirror/class-inl.h" |
| 24 | #include "mirror/field-inl.h" |
| 25 | #include "mirror/object-inl.h" |
| 26 | #include "mirror/object_array-inl.h" |
| 27 | #include "mirror/proxy.h" |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 28 | #include "reflection.h" |
| 29 | #include "scoped_thread_state_change.h" |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 30 | #include "ScopedLocalRef.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 31 | #include "well_known_classes.h" |
TDYa127 | 5bb8601 | 2012-04-11 05:57:28 -0700 | [diff] [blame] | 32 | |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 33 | double art_l2d(int64_t l) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 34 | return static_cast<double>(l); |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | float art_l2f(int64_t l) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 38 | return static_cast<float>(l); |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 39 | } |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 40 | |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 41 | /* |
| 42 | * Float/double conversion requires clamping to min and max of integer form. If |
| 43 | * target doesn't support this normally, use these. |
| 44 | */ |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 45 | int64_t art_d2l(double d) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 46 | static const double kMaxLong = static_cast<double>(static_cast<int64_t>(0x7fffffffffffffffULL)); |
| 47 | static const double kMinLong = static_cast<double>(static_cast<int64_t>(0x8000000000000000ULL)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 48 | if (d >= kMaxLong) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 49 | return static_cast<int64_t>(0x7fffffffffffffffULL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 50 | } else if (d <= kMinLong) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 51 | return static_cast<int64_t>(0x8000000000000000ULL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 52 | } else if (d != d) { // NaN case |
| 53 | return 0; |
| 54 | } else { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 55 | return static_cast<int64_t>(d); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 59 | int64_t art_f2l(float f) { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 60 | static const float kMaxLong = static_cast<float>(static_cast<int64_t>(0x7fffffffffffffffULL)); |
| 61 | static const float kMinLong = static_cast<float>(static_cast<int64_t>(0x8000000000000000ULL)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 62 | if (f >= kMaxLong) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 63 | return static_cast<int64_t>(0x7fffffffffffffffULL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 64 | } else if (f <= kMinLong) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 65 | return static_cast<int64_t>(0x8000000000000000ULL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 66 | } else if (f != f) { // NaN case |
| 67 | return 0; |
| 68 | } else { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 69 | return static_cast<int64_t>(f); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 73 | int32_t art_d2i(double d) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 74 | static const double kMaxInt = static_cast<double>(static_cast<int32_t>(0x7fffffffUL)); |
| 75 | static const double kMinInt = static_cast<double>(static_cast<int32_t>(0x80000000UL)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 76 | if (d >= kMaxInt) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 77 | return static_cast<int32_t>(0x7fffffffUL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 78 | } else if (d <= kMinInt) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 79 | return static_cast<int32_t>(0x80000000UL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 80 | } else if (d != d) { // NaN case |
| 81 | return 0; |
| 82 | } else { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 83 | return static_cast<int32_t>(d); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 87 | int32_t art_f2i(float f) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 88 | static const float kMaxInt = static_cast<float>(static_cast<int32_t>(0x7fffffffUL)); |
| 89 | static const float kMinInt = static_cast<float>(static_cast<int32_t>(0x80000000UL)); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 90 | if (f >= kMaxInt) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 91 | return static_cast<int32_t>(0x7fffffffUL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 92 | } else if (f <= kMinInt) { |
Logan Chien | 008fa51 | 2012-06-22 08:09:57 -0700 | [diff] [blame] | 93 | return static_cast<int32_t>(0x80000000UL); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 94 | } else if (f != f) { // NaN case |
| 95 | return 0; |
| 96 | } else { |
Elliott Hughes | 7484741 | 2012-06-20 18:10:21 -0700 | [diff] [blame] | 97 | return static_cast<int32_t>(f); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
jeffhao | 41005dd | 2012-05-09 17:58:52 -0700 | [diff] [blame] | 101 | namespace art { |
| 102 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 103 | // Helper function to allocate array for FILLED_NEW_ARRAY. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 104 | mirror::Array* CheckAndAllocArrayFromCode(uint32_t type_idx, mirror::AbstractMethod* method, |
| 105 | int32_t component_count, Thread* self, |
| 106 | bool access_check) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 107 | if (UNLIKELY(component_count < 0)) { |
| 108 | self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count); |
| 109 | return NULL; // Failure |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 110 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 111 | mirror::Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 112 | if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve |
| 113 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 114 | if (klass == NULL) { // Error |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 115 | DCHECK(self->IsExceptionPending()); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 116 | return NULL; // Failure |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 117 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 118 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 119 | if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) { |
| 120 | if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 121 | self->ThrowNewExceptionF("Ljava/lang/RuntimeException;", |
| 122 | "Bad filled array request for type %s", |
| 123 | PrettyDescriptor(klass).c_str()); |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 124 | } else { |
Ian Rogers | 50b35e2 | 2012-10-04 10:09:15 -0700 | [diff] [blame] | 125 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 126 | "Found type %s; filled-new-array not implemented for anything but \'int\'", |
| 127 | PrettyDescriptor(klass).c_str()); |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 128 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 129 | return NULL; // Failure |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 130 | } else { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 131 | if (access_check) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 132 | mirror::Class* referrer = method->GetDeclaringClass(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 133 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 134 | ThrowIllegalAccessErrorClass(referrer, klass); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 135 | return NULL; // Failure |
| 136 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 137 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 138 | DCHECK(klass->IsArrayClass()) << PrettyClass(klass); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 139 | return mirror::Array::Alloc(self, klass, component_count); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 143 | mirror::Field* FindFieldFromCode(uint32_t field_idx, const mirror::AbstractMethod* referrer, |
| 144 | Thread* self, FindFieldType type, size_t expected_size) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 145 | bool is_primitive; |
| 146 | bool is_set; |
| 147 | bool is_static; |
| 148 | switch (type) { |
| 149 | case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break; |
| 150 | case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break; |
| 151 | case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break; |
| 152 | case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break; |
| 153 | case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break; |
| 154 | case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break; |
| 155 | case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break; |
| 156 | case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through. |
| 157 | default: is_primitive = true; is_set = true; is_static = true; break; |
| 158 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 159 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 160 | mirror::Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 161 | if (UNLIKELY(resolved_field == NULL)) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 162 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 163 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 164 | } else { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 165 | if (resolved_field->IsStatic() != is_static) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 166 | ThrowIncompatibleClassChangeErrorField(resolved_field, is_static, referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 167 | return NULL; |
| 168 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 169 | mirror::Class* fields_class = resolved_field->GetDeclaringClass(); |
| 170 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 171 | if (UNLIKELY(!referring_class->CanAccess(fields_class) || |
| 172 | !referring_class->CanAccessMember(fields_class, |
| 173 | resolved_field->GetAccessFlags()))) { |
| 174 | // The referring class can't access the resolved field, this may occur as a result of a |
| 175 | // protected field being made public by a sub-class. Resort to the dex file to determine |
| 176 | // the correct class for the access check. |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 177 | const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile(); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 178 | fields_class = class_linker->ResolveType(dex_file, |
| 179 | dex_file.GetFieldId(field_idx).class_idx_, |
| 180 | referring_class); |
| 181 | if (UNLIKELY(!referring_class->CanAccess(fields_class))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 182 | ThrowIllegalAccessErrorClass(referring_class, fields_class); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 183 | return NULL; // failure |
| 184 | } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class, |
| 185 | resolved_field->GetAccessFlags()))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 186 | ThrowIllegalAccessErrorField(referring_class, resolved_field); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 187 | return NULL; // failure |
| 188 | } |
| 189 | } |
| 190 | if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 191 | ThrowIllegalAccessErrorFinalField(referrer, resolved_field); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 192 | return NULL; // failure |
| 193 | } else { |
| 194 | FieldHelper fh(resolved_field); |
| 195 | if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || |
| 196 | fh.FieldSize() != expected_size)) { |
| 197 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 198 | "Attempted read of %zd-bit %s on field '%s'", |
| 199 | expected_size * (32 / sizeof(int32_t)), |
| 200 | is_primitive ? "primitive" : "non-primitive", |
| 201 | PrettyField(resolved_field, true).c_str()); |
| 202 | return NULL; // failure |
| 203 | } else if (!is_static) { |
| 204 | // instance fields must be being accessed on an initialized class |
| 205 | return resolved_field; |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 206 | } else { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 207 | // If the class is already initializing, we must be inside <clinit>, or |
| 208 | // we'd still be waiting for the lock. |
| 209 | if (fields_class->IsInitializing()) { |
| 210 | return resolved_field; |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 211 | } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 212 | return resolved_field; |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 213 | } else { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 214 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind |
| 215 | return NULL; // failure |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 216 | } |
| 217 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Slow path method resolution |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 223 | mirror::AbstractMethod* FindMethodFromCode(uint32_t method_idx, mirror::Object* this_object, |
| 224 | mirror::AbstractMethod* referrer, |
| 225 | Thread* self, bool access_check, InvokeType type) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 226 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 227 | bool is_direct = type == kStatic || type == kDirect; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 228 | mirror::AbstractMethod* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 229 | if (UNLIKELY(resolved_method == NULL)) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 230 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 231 | return NULL; // Failure. |
jeffhao | 262e251 | 2012-12-11 09:46:43 -0800 | [diff] [blame] | 232 | } else if (UNLIKELY(this_object == NULL && type != kStatic)) { |
| 233 | // Maintain interpreter-like semantics where NullPointerException is thrown |
| 234 | // after potential NoSuchMethodError from class linker. |
| 235 | ThrowNullPointerExceptionForMethodAccess(referrer, method_idx, type); |
| 236 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 237 | } else { |
| 238 | if (!access_check) { |
| 239 | if (is_direct) { |
| 240 | return resolved_method; |
| 241 | } else if (type == kInterface) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 242 | mirror::AbstractMethod* interface_method = |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 243 | this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); |
| 244 | if (UNLIKELY(interface_method == NULL)) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 245 | ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object, |
| 246 | referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 247 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 248 | } else { |
| 249 | return interface_method; |
| 250 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 251 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 252 | mirror::ObjectArray<mirror::AbstractMethod>* vtable; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 253 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
| 254 | if (type == kSuper) { |
| 255 | vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable(); |
| 256 | } else { |
| 257 | vtable = this_object->GetClass()->GetVTable(); |
| 258 | } |
| 259 | // TODO: eliminate bounds check? |
| 260 | return vtable->Get(vtable_index); |
| 261 | } |
| 262 | } else { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 263 | // Incompatible class change should have been handled in resolve method. |
| 264 | if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) { |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 265 | ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method, |
| 266 | referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 267 | return NULL; // Failure. |
| 268 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 269 | mirror::Class* methods_class = resolved_method->GetDeclaringClass(); |
| 270 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 271 | if (UNLIKELY(!referring_class->CanAccess(methods_class) || |
| 272 | !referring_class->CanAccessMember(methods_class, |
| 273 | resolved_method->GetAccessFlags()))) { |
| 274 | // The referring class can't access the resolved method, this may occur as a result of a |
| 275 | // protected method being made public by implementing an interface that re-declares the |
| 276 | // 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] | 277 | const DexFile& dex_file = *referring_class->GetDexCache()->GetDexFile(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 278 | methods_class = class_linker->ResolveType(dex_file, |
| 279 | dex_file.GetMethodId(method_idx).class_idx_, |
| 280 | referring_class); |
| 281 | if (UNLIKELY(!referring_class->CanAccess(methods_class))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 282 | ThrowIllegalAccessErrorClassForMethodDispatch(referring_class, methods_class, |
| 283 | referrer, resolved_method, type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 284 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 285 | } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class, |
| 286 | resolved_method->GetAccessFlags()))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 287 | ThrowIllegalAccessErrorMethod(referring_class, resolved_method); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 288 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | if (is_direct) { |
| 292 | return resolved_method; |
| 293 | } else if (type == kInterface) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 294 | mirror::AbstractMethod* interface_method = |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 295 | this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); |
| 296 | if (UNLIKELY(interface_method == NULL)) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 297 | ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(resolved_method, this_object, |
| 298 | referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 299 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 300 | } else { |
| 301 | return interface_method; |
| 302 | } |
| 303 | } else { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 304 | mirror::ObjectArray<mirror::AbstractMethod>* vtable; |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 305 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
| 306 | if (type == kSuper) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 307 | mirror::Class* super_class = referring_class->GetSuperClass(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 308 | if (LIKELY(super_class != NULL)) { |
| 309 | vtable = referring_class->GetSuperClass()->GetVTable(); |
| 310 | } else { |
| 311 | vtable = NULL; |
| 312 | } |
| 313 | } else { |
| 314 | vtable = this_object->GetClass()->GetVTable(); |
| 315 | } |
| 316 | if (LIKELY(vtable != NULL && |
| 317 | vtable_index < static_cast<uint32_t>(vtable->GetLength()))) { |
| 318 | return vtable->GetWithoutChecks(vtable_index); |
| 319 | } else { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 320 | // Behavior to agree with that of the verifier. |
| 321 | MethodHelper mh(resolved_method); |
| 322 | ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(), |
Ian Rogers | 2fc1427 | 2012-08-30 10:56:57 -0700 | [diff] [blame] | 323 | mh.GetSignature(), referrer); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame] | 324 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 325 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 331 | mirror::Class* ResolveVerifyAndClinit(uint32_t type_idx, const mirror::AbstractMethod* referrer, |
| 332 | Thread* self, bool can_run_clinit, bool verify_access) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 333 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 334 | mirror::Class* klass = class_linker->ResolveType(type_idx, referrer); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 335 | if (UNLIKELY(klass == NULL)) { |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 336 | CHECK(self->IsExceptionPending()); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 337 | return NULL; // Failure - Indicate to caller to deliver exception |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 338 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 339 | // Perform access check if necessary. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 340 | mirror::Class* referring_class = referrer->GetDeclaringClass(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 341 | if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 342 | ThrowIllegalAccessErrorClass(referring_class, klass); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 343 | return NULL; // Failure - Indicate to caller to deliver exception |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 344 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 345 | // If we're just implementing const-class, we shouldn't call <clinit>. |
| 346 | if (!can_run_clinit) { |
| 347 | return klass; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 348 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 349 | // If we are the <clinit> of this class, just return our storage. |
| 350 | // |
| 351 | // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished |
| 352 | // running. |
| 353 | if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) { |
| 354 | return klass; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 355 | } |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 356 | if (!class_linker->EnsureInitialized(klass, true, true)) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 357 | CHECK(self->IsExceptionPending()); |
| 358 | return NULL; // Failure - Indicate to caller to deliver exception |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 359 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 360 | referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass); |
| 361 | return klass; |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 362 | } |
| 363 | |
jeffhao | d752132 | 2012-11-21 15:38:24 -0800 | [diff] [blame] | 364 | void ThrowStackOverflowError(Thread* self) { |
| 365 | CHECK(!self->IsHandlingStackOverflow()) << "Recursive stack overflow."; |
| 366 | // Remove extra entry pushed onto second stack during method tracing. |
| 367 | if (Runtime::Current()->IsMethodTracingActive()) { |
| 368 | InstrumentationMethodUnwindFromCode(self); |
| 369 | } |
| 370 | self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute. |
| 371 | JNIEnvExt* env = self->GetJniEnv(); |
| 372 | std::string msg("stack size "); |
| 373 | msg += PrettySize(self->GetStackSize()); |
| 374 | // Use low-level JNI routine and pre-baked error class to avoid class linking operations that |
| 375 | // would consume more stack. |
| 376 | int rc = ::art::ThrowNewException(env, WellKnownClasses::java_lang_StackOverflowError, |
| 377 | msg.c_str(), NULL); |
| 378 | if (rc != JNI_OK) { |
| 379 | // TODO: ThrowNewException failed presumably because of an OOME, we continue to throw the OOME |
| 380 | // or die in the CHECK below. We may want to throw a pre-baked StackOverflowError |
| 381 | // instead. |
| 382 | LOG(ERROR) << "Couldn't throw new StackOverflowError because JNI ThrowNew failed."; |
| 383 | CHECK(self->IsExceptionPending()); |
| 384 | } |
| 385 | self->ResetDefaultStackEnd(); // Return to default stack size. |
| 386 | } |
| 387 | |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 388 | JValue InvokeProxyInvocationHandler(ScopedObjectAccessUnchecked& soa, const char* shorty, |
| 389 | jobject rcvr_jobj, jobject interface_method_jobj, |
| 390 | std::vector<jvalue>& args) { |
| 391 | DCHECK(soa.Env()->IsInstanceOf(rcvr_jobj, WellKnownClasses::java_lang_reflect_Proxy)); |
| 392 | |
| 393 | // Build argument array possibly triggering GC. |
| 394 | soa.Self()->AssertThreadSuspensionIsAllowable(); |
| 395 | jobjectArray args_jobj = NULL; |
| 396 | const JValue zero; |
| 397 | if (args.size() > 0) { |
| 398 | args_jobj = soa.Env()->NewObjectArray(args.size(), WellKnownClasses::java_lang_Object, NULL); |
| 399 | if (args_jobj == NULL) { |
| 400 | CHECK(soa.Self()->IsExceptionPending()); |
| 401 | return zero; |
| 402 | } |
| 403 | for (size_t i = 0; i < args.size(); ++i) { |
| 404 | if (shorty[i + 1] == 'L') { |
| 405 | jobject val = args.at(i).l; |
| 406 | soa.Env()->SetObjectArrayElement(args_jobj, i, val); |
| 407 | } else { |
| 408 | JValue jv; |
| 409 | jv.SetJ(args.at(i).j); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 410 | mirror::Object* val = BoxPrimitive(Primitive::GetType(shorty[i + 1]), jv); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 411 | if (val == NULL) { |
| 412 | CHECK(soa.Self()->IsExceptionPending()); |
| 413 | return zero; |
| 414 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 415 | soa.Decode<mirror::ObjectArray<mirror::Object>* >(args_jobj)->Set(i, val); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // Call InvocationHandler.invoke(Object proxy, Method method, Object[] args). |
| 421 | jobject inv_hand = soa.Env()->GetObjectField(rcvr_jobj, |
| 422 | WellKnownClasses::java_lang_reflect_Proxy_h); |
| 423 | jvalue invocation_args[3]; |
| 424 | invocation_args[0].l = rcvr_jobj; |
| 425 | invocation_args[1].l = interface_method_jobj; |
| 426 | invocation_args[2].l = args_jobj; |
| 427 | jobject result = |
| 428 | soa.Env()->CallObjectMethodA(inv_hand, |
| 429 | WellKnownClasses::java_lang_reflect_InvocationHandler_invoke, |
| 430 | invocation_args); |
| 431 | |
| 432 | // Unbox result and handle error conditions. |
| 433 | if (!soa.Self()->IsExceptionPending()) { |
| 434 | if (shorty[0] == 'V' || result == NULL) { |
| 435 | // Do nothing. |
| 436 | return zero; |
| 437 | } else { |
| 438 | JValue result_unboxed; |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 439 | MethodHelper mh(soa.Decode<mirror::AbstractMethod*>(interface_method_jobj)); |
| 440 | mirror::Class* result_type = mh.GetReturnType(); |
| 441 | mirror::Object* result_ref = soa.Decode<mirror::Object*>(result); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 442 | bool unboxed_okay = UnboxPrimitiveForResult(result_ref, result_type, result_unboxed); |
| 443 | if (!unboxed_okay) { |
Ian Rogers | 530f71c | 2013-02-22 23:29:00 -0800 | [diff] [blame] | 444 | // UnboxPrimitiveForResult creates an IllegalArgumentException. Discard and create a |
| 445 | // meaningful ClassCastException. |
| 446 | DCHECK(soa.Self()->IsExceptionPending()); |
| 447 | soa.Self()->ClearException(); |
| 448 | soa.Self()->ThrowNewException("Ljava/lang/ClassCastException;", |
| 449 | StringPrintf("Couldn't convert result of type %s to %s", |
| 450 | PrettyTypeOf(result_ref).c_str(), |
| 451 | PrettyDescriptor(result_type).c_str() |
| 452 | ).c_str()); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 453 | } |
| 454 | return result_unboxed; |
| 455 | } |
| 456 | } else { |
| 457 | // In the case of checked exceptions that aren't declared, the exception must be wrapped by |
| 458 | // a UndeclaredThrowableException. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 459 | mirror::Throwable* exception = soa.Self()->GetException(); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 460 | if (exception->IsCheckedException()) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 461 | mirror::Object* rcvr = soa.Decode<mirror::Object*>(rcvr_jobj); |
| 462 | mirror::SynthesizedProxyClass* proxy_class = |
| 463 | down_cast<mirror::SynthesizedProxyClass*>(rcvr->GetClass()); |
| 464 | mirror::AbstractMethod* interface_method = |
| 465 | soa.Decode<mirror::AbstractMethod*>(interface_method_jobj); |
| 466 | mirror::AbstractMethod* proxy_method = |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 467 | rcvr->GetClass()->FindVirtualMethodForInterface(interface_method); |
| 468 | int throws_index = -1; |
| 469 | size_t num_virt_methods = proxy_class->NumVirtualMethods(); |
| 470 | for (size_t i = 0; i < num_virt_methods; i++) { |
| 471 | if (proxy_class->GetVirtualMethod(i) == proxy_method) { |
| 472 | throws_index = i; |
| 473 | break; |
| 474 | } |
| 475 | } |
| 476 | CHECK_NE(throws_index, -1); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 477 | mirror::ObjectArray<mirror::Class>* declared_exceptions = proxy_class->GetThrows()->Get(throws_index); |
| 478 | mirror::Class* exception_class = exception->GetClass(); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 479 | bool declares_exception = false; |
| 480 | for (int i = 0; i < declared_exceptions->GetLength() && !declares_exception; i++) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 481 | mirror::Class* declared_exception = declared_exceptions->Get(i); |
Ian Rogers | af6e67a | 2013-01-16 08:38:37 -0800 | [diff] [blame] | 482 | declares_exception = declared_exception->IsAssignableFrom(exception_class); |
| 483 | } |
| 484 | if (!declares_exception) { |
| 485 | soa.Self()->ThrowNewWrappedException("Ljava/lang/reflect/UndeclaredThrowableException;", |
| 486 | NULL); |
| 487 | } |
| 488 | } |
| 489 | return zero; |
| 490 | } |
| 491 | } |
| 492 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 493 | } // namespace art |