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 | void ThrowNewIllegalAccessErrorClass(Thread* self, |
| 93 | Class* referrer, |
| 94 | Class* accessed) { |
| 95 | self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 96 | "Illegal class access: '%s' -> '%s'", |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 97 | PrettyDescriptor(referrer).c_str(), |
| 98 | PrettyDescriptor(accessed).c_str()); |
Shih-wei Liao | ddbd01a | 2012-03-09 14:42:12 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 101 | void ThrowNewIllegalAccessErrorClassForMethodDispatch(Thread* self, |
| 102 | Class* referrer, |
| 103 | Class* accessed, |
| 104 | const Method* caller, |
| 105 | const Method* called, |
| 106 | InvokeType type) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 107 | self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 108 | "Illegal class access ('%s' -> '%s')" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 109 | "in attempt to invoke %s method '%s' from '%s'", |
| 110 | PrettyDescriptor(referrer).c_str(), |
| 111 | PrettyDescriptor(accessed).c_str(), |
Elliott Hughes | 6fcce30 | 2012-06-19 16:54:19 -0700 | [diff] [blame] | 112 | ToStr<InvokeType>(type).c_str(), |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 113 | PrettyMethod(called).c_str(), |
| 114 | PrettyMethod(caller).c_str()); |
buzbee | 44b412b | 2012-02-04 08:50:53 -0800 | [diff] [blame] | 115 | } |
| 116 | |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 117 | static void ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(Thread* self, |
| 118 | const Method* interface_method, |
| 119 | Object* this_object) |
| 120 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
| 121 | std::string interface_method_name(PrettyMethod(interface_method)); |
| 122 | if (this_object != NULL) { |
| 123 | std::string this_class_descriptor(PrettyDescriptor(this_object->GetClass())); |
| 124 | std::string interface_class_descriptor(PrettyDescriptor(interface_method->GetDeclaringClass())); |
| 125 | self->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;", |
| 126 | "Class '%s' does not implement interface '%s' in call to '%s'", |
| 127 | this_class_descriptor.c_str(), |
| 128 | interface_class_descriptor.c_str(), |
| 129 | interface_method_name.c_str()); |
| 130 | } else { |
| 131 | self->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;", |
| 132 | "Expected '%s' to be an interface method", |
| 133 | interface_method_name.c_str()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void ThrowNewIncompatibleClassChangeErrorField(Thread* self, const Field* resolved_field, |
| 138 | bool is_static) |
| 139 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 140 | self->ThrowNewExceptionF("Ljava/lang/IncompatibleClassChangeError;", |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 141 | "Expected '%s' to be a %s field", |
| 142 | PrettyField(resolved_field).c_str(), |
| 143 | is_static ? "static" : "instance"); |
| 144 | } |
| 145 | |
| 146 | void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type, |
| 147 | Method* method) { |
| 148 | std::ostringstream msg; |
| 149 | msg << "The method '" << PrettyMethod(method) << "' was expected to be of type" |
| 150 | << expected_type << " but instead was found to be of type " << found_type; |
| 151 | ClassHelper kh(method->GetDeclaringClass()); |
| 152 | std::string location(kh.GetLocation()); |
| 153 | if (!location.empty()) { |
| 154 | msg << " (accessed from " << location << ")"; |
| 155 | } |
| 156 | Thread::Current()->ThrowNewException("Ljava/lang/IncompatibleClassChangeError;", |
| 157 | msg.str().c_str()); |
| 158 | } |
| 159 | |
| 160 | void ThrowNoSuchMethodError(InvokeType type, Class* c, const StringPiece& name, |
| 161 | const StringPiece& signature) { |
| 162 | ClassHelper kh(c); |
| 163 | std::ostringstream msg; |
| 164 | msg << "No " << type << " method " << name << signature |
| 165 | << " in class " << kh.GetDescriptor() << " or its superclasses"; |
| 166 | std::string location(kh.GetLocation()); |
| 167 | if (!location.empty()) { |
| 168 | msg << " (accessed from " << location << ")"; |
| 169 | } |
| 170 | Thread::Current()->ThrowNewException("Ljava/lang/NoSuchMethodError;", msg.str().c_str()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 173 | void ThrowNewIllegalAccessErrorField(Thread* self, |
| 174 | Class* referrer, |
| 175 | Field* accessed) { |
| 176 | self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", |
| 177 | "Field '%s' is inaccessible to class '%s'", |
| 178 | PrettyField(accessed, false).c_str(), |
| 179 | PrettyDescriptor(referrer).c_str()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 182 | void ThrowNewIllegalAccessErrorFinalField(Thread* self, |
| 183 | const Method* referrer, |
| 184 | Field* accessed) { |
| 185 | self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", |
| 186 | "Final field '%s' cannot be written to by method '%s'", |
| 187 | PrettyField(accessed, false).c_str(), |
| 188 | PrettyMethod(referrer).c_str()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 191 | void ThrowNewIllegalAccessErrorMethod(Thread* self, |
| 192 | Class* referrer, |
| 193 | Method* accessed) { |
| 194 | self->ThrowNewExceptionF("Ljava/lang/IllegalAccessError;", |
| 195 | "Method '%s' is inaccessible to class '%s'", |
| 196 | PrettyMethod(accessed).c_str(), |
| 197 | PrettyDescriptor(referrer).c_str()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 200 | void ThrowNullPointerExceptionForFieldAccess(Thread* self, |
| 201 | Field* field, |
| 202 | bool is_read) { |
| 203 | self->ThrowNewExceptionF("Ljava/lang/NullPointerException;", |
| 204 | "Attempt to %s field '%s' on a null object reference", |
| 205 | is_read ? "read from" : "write to", |
| 206 | PrettyField(field, true).c_str()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 209 | void ThrowNullPointerExceptionForMethodAccess(Thread* self, |
| 210 | Method* caller, |
| 211 | uint32_t method_idx, |
| 212 | InvokeType type) { |
| 213 | const DexFile& dex_file = |
| 214 | Runtime::Current()->GetClassLinker()->FindDexFile(caller->GetDeclaringClass()->GetDexCache()); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 215 | self->ThrowNewExceptionF("Ljava/lang/NullPointerException;", |
| 216 | "Attempt to invoke %s method '%s' on a null object reference", |
Elliott Hughes | 6fcce30 | 2012-06-19 16:54:19 -0700 | [diff] [blame] | 217 | ToStr<InvokeType>(type).c_str(), |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 218 | PrettyMethod(method_idx, dex_file, true).c_str()); |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 219 | } |
| 220 | |
TDYa127 | 3f9137d | 2012-04-08 15:59:19 -0700 | [diff] [blame] | 221 | void ThrowNullPointerExceptionFromDexPC(Thread* self, Method* throw_method, uint32_t dex_pc) { |
| 222 | const DexFile::CodeItem* code = MethodHelper(throw_method).GetCodeItem(); |
| 223 | CHECK_LT(dex_pc, code->insns_size_in_code_units_); |
| 224 | const Instruction* instr = Instruction::At(&code->insns_[dex_pc]); |
| 225 | DecodedInstruction dec_insn(instr); |
| 226 | switch (instr->Opcode()) { |
| 227 | case Instruction::INVOKE_DIRECT: |
| 228 | case Instruction::INVOKE_DIRECT_RANGE: |
| 229 | ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kDirect); |
| 230 | break; |
| 231 | case Instruction::INVOKE_VIRTUAL: |
| 232 | case Instruction::INVOKE_VIRTUAL_RANGE: |
| 233 | ThrowNullPointerExceptionForMethodAccess(self, throw_method, dec_insn.vB, kVirtual); |
| 234 | break; |
| 235 | case Instruction::IGET: |
| 236 | case Instruction::IGET_WIDE: |
| 237 | case Instruction::IGET_OBJECT: |
| 238 | case Instruction::IGET_BOOLEAN: |
| 239 | case Instruction::IGET_BYTE: |
| 240 | case Instruction::IGET_CHAR: |
| 241 | case Instruction::IGET_SHORT: { |
| 242 | Field* field = |
| 243 | Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false); |
| 244 | ThrowNullPointerExceptionForFieldAccess(self, field, true /* read */); |
| 245 | break; |
| 246 | } |
| 247 | case Instruction::IPUT: |
| 248 | case Instruction::IPUT_WIDE: |
| 249 | case Instruction::IPUT_OBJECT: |
| 250 | case Instruction::IPUT_BOOLEAN: |
| 251 | case Instruction::IPUT_BYTE: |
| 252 | case Instruction::IPUT_CHAR: |
| 253 | case Instruction::IPUT_SHORT: { |
| 254 | Field* field = |
| 255 | Runtime::Current()->GetClassLinker()->ResolveField(dec_insn.vC, throw_method, false); |
| 256 | ThrowNullPointerExceptionForFieldAccess(self, field, false /* write */); |
| 257 | break; |
| 258 | } |
| 259 | case Instruction::AGET: |
| 260 | case Instruction::AGET_WIDE: |
| 261 | case Instruction::AGET_OBJECT: |
| 262 | case Instruction::AGET_BOOLEAN: |
| 263 | case Instruction::AGET_BYTE: |
| 264 | case Instruction::AGET_CHAR: |
| 265 | case Instruction::AGET_SHORT: |
| 266 | self->ThrowNewException("Ljava/lang/NullPointerException;", |
| 267 | "Attempt to read from null array"); |
| 268 | break; |
| 269 | case Instruction::APUT: |
| 270 | case Instruction::APUT_WIDE: |
| 271 | case Instruction::APUT_OBJECT: |
| 272 | case Instruction::APUT_BOOLEAN: |
| 273 | case Instruction::APUT_BYTE: |
| 274 | case Instruction::APUT_CHAR: |
| 275 | case Instruction::APUT_SHORT: |
| 276 | self->ThrowNewException("Ljava/lang/NullPointerException;", |
| 277 | "Attempt to write to null array"); |
| 278 | break; |
Elliott Hughes | 6fcce30 | 2012-06-19 16:54:19 -0700 | [diff] [blame] | 279 | case Instruction::ARRAY_LENGTH: |
| 280 | self->ThrowNewException("Ljava/lang/NullPointerException;", |
| 281 | "Attempt to get length of null array"); |
| 282 | break; |
TDYa127 | 3f9137d | 2012-04-08 15:59:19 -0700 | [diff] [blame] | 283 | default: { |
| 284 | const DexFile& dex_file = Runtime::Current()->GetClassLinker() |
| 285 | ->FindDexFile(throw_method->GetDeclaringClass()->GetDexCache()); |
| 286 | std::string message("Null pointer exception during instruction '"); |
| 287 | message += instr->DumpString(&dex_file); |
| 288 | message += "'"; |
| 289 | self->ThrowNewException("Ljava/lang/NullPointerException;", message.c_str()); |
| 290 | break; |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 295 | std::string FieldNameFromIndex(const Method* method, uint32_t ref, |
| 296 | verifier::VerifyErrorRefType ref_type, bool access) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 297 | CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_FIELD)); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 298 | |
| 299 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 300 | const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
| 301 | |
| 302 | const DexFile::FieldId& id = dex_file.GetFieldId(ref); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 303 | std::string class_name(PrettyDescriptor(dex_file.GetFieldDeclaringClassDescriptor(id))); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 304 | const char* field_name = dex_file.StringDataByIdx(id.name_idx_); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 305 | if (!access) { |
| 306 | return class_name + "." + field_name; |
| 307 | } |
| 308 | |
| 309 | std::string result; |
| 310 | result += "tried to access field "; |
| 311 | result += class_name + "." + field_name; |
| 312 | result += " from class "; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 313 | result += PrettyDescriptor(method->GetDeclaringClass()); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 314 | return result; |
| 315 | } |
| 316 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 317 | std::string MethodNameFromIndex(const Method* method, uint32_t ref, |
| 318 | verifier::VerifyErrorRefType ref_type, bool access) { |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 319 | CHECK_EQ(static_cast<int>(ref_type), static_cast<int>(verifier::VERIFY_ERROR_REF_METHOD)); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 320 | |
| 321 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 322 | const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
| 323 | |
| 324 | const DexFile::MethodId& id = dex_file.GetMethodId(ref); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 325 | std::string class_name(PrettyDescriptor(dex_file.GetMethodDeclaringClassDescriptor(id))); |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 326 | const char* method_name = dex_file.StringDataByIdx(id.name_idx_); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 327 | if (!access) { |
| 328 | return class_name + "." + method_name; |
| 329 | } |
| 330 | |
| 331 | std::string result; |
| 332 | result += "tried to access method "; |
Ian Rogers | 4f0d07c | 2011-10-06 23:38:47 -0700 | [diff] [blame] | 333 | result += class_name + "." + method_name + ":" + |
Ian Rogers | 0571d35 | 2011-11-03 19:51:38 -0700 | [diff] [blame] | 334 | dex_file.CreateMethodSignature(id.proto_idx_, NULL); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 335 | result += " from class "; |
Ian Rogers | 6d4d9fc | 2011-11-30 16:24:48 -0800 | [diff] [blame] | 336 | result += PrettyDescriptor(method->GetDeclaringClass()); |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 337 | return result; |
| 338 | } |
| 339 | |
TDYa127 | b92bcab | 2012-04-08 00:09:51 -0700 | [diff] [blame] | 340 | static std::string ClassNameFromIndex(const Method* method, uint32_t ref, |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 341 | verifier::VerifyErrorRefType ref_type, bool access) |
| 342 | SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) { |
Logan Chien | 9e5f5c1 | 2012-04-10 13:51:45 +0800 | [diff] [blame] | 343 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 344 | const DexFile& dex_file = class_linker->FindDexFile(method->GetDeclaringClass()->GetDexCache()); |
| 345 | |
| 346 | uint16_t type_idx = 0; |
| 347 | if (ref_type == verifier::VERIFY_ERROR_REF_FIELD) { |
| 348 | const DexFile::FieldId& id = dex_file.GetFieldId(ref); |
| 349 | type_idx = id.class_idx_; |
| 350 | } else if (ref_type == verifier::VERIFY_ERROR_REF_METHOD) { |
| 351 | const DexFile::MethodId& id = dex_file.GetMethodId(ref); |
| 352 | type_idx = id.class_idx_; |
| 353 | } else if (ref_type == verifier::VERIFY_ERROR_REF_CLASS) { |
| 354 | type_idx = ref; |
| 355 | } else { |
| 356 | CHECK(false) << static_cast<int>(ref_type); |
| 357 | } |
| 358 | |
| 359 | std::string class_name(PrettyDescriptor(dex_file.StringByTypeIdx(type_idx))); |
| 360 | if (!access) { |
| 361 | return class_name; |
| 362 | } |
| 363 | |
| 364 | std::string result; |
| 365 | result += "tried to access class "; |
| 366 | result += class_name; |
| 367 | result += " from class "; |
| 368 | result += PrettyDescriptor(method->GetDeclaringClass()); |
| 369 | return result; |
| 370 | } |
| 371 | |
| 372 | void ThrowVerificationError(Thread* self, const Method* method, |
| 373 | int32_t kind, int32_t ref) { |
| 374 | verifier::VerifyErrorRefType ref_type = |
| 375 | static_cast<verifier::VerifyErrorRefType>(kind >> verifier::kVerifyErrorRefTypeShift); |
| 376 | |
| 377 | const char* exception_class = "Ljava/lang/VerifyError;"; |
| 378 | std::string msg; |
| 379 | |
| 380 | switch (static_cast<verifier::VerifyError>(kind & ~(0xff << verifier::kVerifyErrorRefTypeShift))) { |
| 381 | case verifier::VERIFY_ERROR_NO_CLASS: |
| 382 | exception_class = "Ljava/lang/NoClassDefFoundError;"; |
| 383 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 384 | break; |
| 385 | case verifier::VERIFY_ERROR_NO_FIELD: |
| 386 | exception_class = "Ljava/lang/NoSuchFieldError;"; |
| 387 | msg = FieldNameFromIndex(method, ref, ref_type, false); |
| 388 | break; |
| 389 | case verifier::VERIFY_ERROR_NO_METHOD: |
| 390 | exception_class = "Ljava/lang/NoSuchMethodError;"; |
| 391 | msg = MethodNameFromIndex(method, ref, ref_type, false); |
| 392 | break; |
| 393 | case verifier::VERIFY_ERROR_ACCESS_CLASS: |
| 394 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 395 | msg = ClassNameFromIndex(method, ref, ref_type, true); |
| 396 | break; |
| 397 | case verifier::VERIFY_ERROR_ACCESS_FIELD: |
| 398 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 399 | msg = FieldNameFromIndex(method, ref, ref_type, true); |
| 400 | break; |
| 401 | case verifier::VERIFY_ERROR_ACCESS_METHOD: |
| 402 | exception_class = "Ljava/lang/IllegalAccessError;"; |
| 403 | msg = MethodNameFromIndex(method, ref, ref_type, true); |
| 404 | break; |
| 405 | case verifier::VERIFY_ERROR_CLASS_CHANGE: |
| 406 | exception_class = "Ljava/lang/IncompatibleClassChangeError;"; |
| 407 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 408 | break; |
| 409 | case verifier::VERIFY_ERROR_INSTANTIATION: |
| 410 | exception_class = "Ljava/lang/InstantiationError;"; |
| 411 | msg = ClassNameFromIndex(method, ref, ref_type, false); |
| 412 | break; |
| 413 | case verifier::VERIFY_ERROR_BAD_CLASS_SOFT: |
| 414 | case verifier::VERIFY_ERROR_BAD_CLASS_HARD: |
| 415 | // Generic VerifyError; use default exception, no message. |
| 416 | break; |
Logan Chien | 9e5f5c1 | 2012-04-10 13:51:45 +0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | self->ThrowNewException(exception_class, msg.c_str()); |
| 420 | } |
| 421 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 422 | // Helper function to allocate array for FILLED_NEW_ARRAY. |
| 423 | Array* CheckAndAllocArrayFromCode(uint32_t type_idx, Method* method, int32_t component_count, |
| 424 | Thread* self, bool access_check) { |
| 425 | if (UNLIKELY(component_count < 0)) { |
| 426 | self->ThrowNewExceptionF("Ljava/lang/NegativeArraySizeException;", "%d", component_count); |
| 427 | return NULL; // Failure |
Elliott Hughes | 6c8867d | 2011-10-03 16:34:05 -0700 | [diff] [blame] | 428 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 429 | Class* klass = method->GetDexCacheResolvedTypes()->Get(type_idx); |
| 430 | if (UNLIKELY(klass == NULL)) { // Not in dex cache so try to resolve |
| 431 | klass = Runtime::Current()->GetClassLinker()->ResolveType(type_idx, method); |
| 432 | if (klass == NULL) { // Error |
| 433 | DCHECK(Thread::Current()->IsExceptionPending()); |
| 434 | return NULL; // Failure |
Ian Rogers | 1984651 | 2012-02-24 11:42:47 -0800 | [diff] [blame] | 435 | } |
Ian Rogers | ea2a11d | 2011-10-11 16:48:51 -0700 | [diff] [blame] | 436 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 437 | if (UNLIKELY(klass->IsPrimitive() && !klass->IsPrimitiveInt())) { |
| 438 | if (klass->IsPrimitiveLong() || klass->IsPrimitiveDouble()) { |
| 439 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", |
| 440 | "Bad filled array request for type %s", |
| 441 | PrettyDescriptor(klass).c_str()); |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 442 | } else { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 443 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 444 | "Found type %s; filled-new-array not implemented for anything but \'int\'", |
| 445 | PrettyDescriptor(klass).c_str()); |
Ian Rogers | 573db4a | 2011-12-13 15:30:50 -0800 | [diff] [blame] | 446 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 447 | return NULL; // Failure |
Ian Rogers | ad25ac5 | 2011-10-04 19:13:33 -0700 | [diff] [blame] | 448 | } else { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 449 | if (access_check) { |
| 450 | Class* referrer = method->GetDeclaringClass(); |
| 451 | if (UNLIKELY(!referrer->CanAccess(klass))) { |
| 452 | ThrowNewIllegalAccessErrorClass(self, referrer, klass); |
| 453 | return NULL; // Failure |
| 454 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 455 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 456 | DCHECK(klass->IsArrayClass()) << PrettyClass(klass); |
| 457 | return Array::Alloc(klass, component_count); |
| 458 | } |
| 459 | } |
| 460 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 461 | Field* FindFieldFromCode(uint32_t field_idx, const Method* referrer, Thread* self, |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 462 | FindFieldType type, size_t expected_size) { |
| 463 | bool is_primitive; |
| 464 | bool is_set; |
| 465 | bool is_static; |
| 466 | switch (type) { |
| 467 | case InstanceObjectRead: is_primitive = false; is_set = false; is_static = false; break; |
| 468 | case InstanceObjectWrite: is_primitive = false; is_set = true; is_static = false; break; |
| 469 | case InstancePrimitiveRead: is_primitive = true; is_set = false; is_static = false; break; |
| 470 | case InstancePrimitiveWrite: is_primitive = true; is_set = true; is_static = false; break; |
| 471 | case StaticObjectRead: is_primitive = false; is_set = false; is_static = true; break; |
| 472 | case StaticObjectWrite: is_primitive = false; is_set = true; is_static = true; break; |
| 473 | case StaticPrimitiveRead: is_primitive = true; is_set = false; is_static = true; break; |
| 474 | case StaticPrimitiveWrite: // Keep GCC happy by having a default handler, fall-through. |
| 475 | default: is_primitive = true; is_set = true; is_static = true; break; |
| 476 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 477 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 478 | Field* resolved_field = class_linker->ResolveField(field_idx, referrer, is_static); |
| 479 | if (UNLIKELY(resolved_field == NULL)) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 480 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 481 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 482 | } else { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 483 | if (resolved_field->IsStatic() != is_static) { |
| 484 | ThrowNewIncompatibleClassChangeErrorField(self, resolved_field, is_static); |
| 485 | return NULL; |
| 486 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 487 | Class* fields_class = resolved_field->GetDeclaringClass(); |
| 488 | Class* referring_class = referrer->GetDeclaringClass(); |
Ian Rogers | e2645d3 | 2012-04-11 14:42:42 -0700 | [diff] [blame] | 489 | if (UNLIKELY(!referring_class->CanAccess(fields_class) || |
| 490 | !referring_class->CanAccessMember(fields_class, |
| 491 | resolved_field->GetAccessFlags()))) { |
| 492 | // The referring class can't access the resolved field, this may occur as a result of a |
| 493 | // protected field being made public by a sub-class. Resort to the dex file to determine |
| 494 | // the correct class for the access check. |
| 495 | const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache()); |
| 496 | fields_class = class_linker->ResolveType(dex_file, |
| 497 | dex_file.GetFieldId(field_idx).class_idx_, |
| 498 | referring_class); |
| 499 | if (UNLIKELY(!referring_class->CanAccess(fields_class))) { |
| 500 | ThrowNewIllegalAccessErrorClass(self, referring_class, fields_class); |
| 501 | return NULL; // failure |
| 502 | } else if (UNLIKELY(!referring_class->CanAccessMember(fields_class, |
| 503 | resolved_field->GetAccessFlags()))) { |
| 504 | ThrowNewIllegalAccessErrorField(self, referring_class, resolved_field); |
| 505 | return NULL; // failure |
| 506 | } |
| 507 | } |
| 508 | if (UNLIKELY(is_set && resolved_field->IsFinal() && (fields_class != referring_class))) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 509 | ThrowNewIllegalAccessErrorFinalField(self, referrer, resolved_field); |
| 510 | return NULL; // failure |
| 511 | } else { |
| 512 | FieldHelper fh(resolved_field); |
| 513 | if (UNLIKELY(fh.IsPrimitiveType() != is_primitive || |
| 514 | fh.FieldSize() != expected_size)) { |
| 515 | self->ThrowNewExceptionF("Ljava/lang/NoSuchFieldError;", |
| 516 | "Attempted read of %zd-bit %s on field '%s'", |
| 517 | expected_size * (32 / sizeof(int32_t)), |
| 518 | is_primitive ? "primitive" : "non-primitive", |
| 519 | PrettyField(resolved_field, true).c_str()); |
| 520 | return NULL; // failure |
| 521 | } else if (!is_static) { |
| 522 | // instance fields must be being accessed on an initialized class |
| 523 | return resolved_field; |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 524 | } else { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 525 | // If the class is already initializing, we must be inside <clinit>, or |
| 526 | // we'd still be waiting for the lock. |
| 527 | if (fields_class->IsInitializing()) { |
| 528 | return resolved_field; |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 529 | } else if (Runtime::Current()->GetClassLinker()->EnsureInitialized(fields_class, true, true)) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 530 | return resolved_field; |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 531 | } else { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 532 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind |
| 533 | return NULL; // failure |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 534 | } |
| 535 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | // Slow path method resolution |
| 541 | Method* FindMethodFromCode(uint32_t method_idx, Object* this_object, const Method* referrer, |
| 542 | Thread* self, bool access_check, InvokeType type) { |
| 543 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 544 | bool is_direct = type == kStatic || type == kDirect; |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 545 | Method* resolved_method = class_linker->ResolveMethod(method_idx, referrer, type); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 546 | if (UNLIKELY(resolved_method == NULL)) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 547 | DCHECK(self->IsExceptionPending()); // Throw exception and unwind. |
| 548 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 549 | } else { |
| 550 | if (!access_check) { |
| 551 | if (is_direct) { |
| 552 | return resolved_method; |
| 553 | } else if (type == kInterface) { |
| 554 | Method* interface_method = |
| 555 | this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); |
| 556 | if (UNLIKELY(interface_method == NULL)) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 557 | ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 558 | resolved_method, |
| 559 | this_object); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 560 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 561 | } else { |
| 562 | return interface_method; |
| 563 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 564 | } else { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 565 | ObjectArray<Method>* vtable; |
| 566 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
| 567 | if (type == kSuper) { |
| 568 | vtable = referrer->GetDeclaringClass()->GetSuperClass()->GetVTable(); |
| 569 | } else { |
| 570 | vtable = this_object->GetClass()->GetVTable(); |
| 571 | } |
| 572 | // TODO: eliminate bounds check? |
| 573 | return vtable->Get(vtable_index); |
| 574 | } |
| 575 | } else { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 576 | // Incompatible class change should have been handled in resolve method. |
| 577 | if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(type))) { |
| 578 | ThrowIncompatibleClassChangeError(type, resolved_method->GetInvokeType(), resolved_method); |
| 579 | return NULL; // Failure. |
| 580 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 581 | Class* methods_class = resolved_method->GetDeclaringClass(); |
| 582 | Class* referring_class = referrer->GetDeclaringClass(); |
| 583 | if (UNLIKELY(!referring_class->CanAccess(methods_class) || |
| 584 | !referring_class->CanAccessMember(methods_class, |
| 585 | resolved_method->GetAccessFlags()))) { |
| 586 | // The referring class can't access the resolved method, this may occur as a result of a |
| 587 | // protected method being made public by implementing an interface that re-declares the |
| 588 | // method public. Resort to the dex file to determine the correct class for the access check |
| 589 | const DexFile& dex_file = class_linker->FindDexFile(referring_class->GetDexCache()); |
| 590 | methods_class = class_linker->ResolveType(dex_file, |
| 591 | dex_file.GetMethodId(method_idx).class_idx_, |
| 592 | referring_class); |
| 593 | if (UNLIKELY(!referring_class->CanAccess(methods_class))) { |
| 594 | ThrowNewIllegalAccessErrorClassForMethodDispatch(self, referring_class, methods_class, |
| 595 | referrer, resolved_method, type); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 596 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 597 | } else if (UNLIKELY(!referring_class->CanAccessMember(methods_class, |
| 598 | resolved_method->GetAccessFlags()))) { |
| 599 | ThrowNewIllegalAccessErrorMethod(self, referring_class, resolved_method); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 600 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 601 | } |
| 602 | } |
| 603 | if (is_direct) { |
| 604 | return resolved_method; |
| 605 | } else if (type == kInterface) { |
| 606 | Method* interface_method = |
| 607 | this_object->GetClass()->FindVirtualMethodForInterface(resolved_method); |
| 608 | if (UNLIKELY(interface_method == NULL)) { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 609 | ThrowNewIncompatibleClassChangeErrorClassForInterfaceDispatch(self, |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 610 | resolved_method, |
| 611 | this_object); |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 612 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 613 | } else { |
| 614 | return interface_method; |
| 615 | } |
| 616 | } else { |
| 617 | ObjectArray<Method>* vtable; |
| 618 | uint16_t vtable_index = resolved_method->GetMethodIndex(); |
| 619 | if (type == kSuper) { |
| 620 | Class* super_class = referring_class->GetSuperClass(); |
| 621 | if (LIKELY(super_class != NULL)) { |
| 622 | vtable = referring_class->GetSuperClass()->GetVTable(); |
| 623 | } else { |
| 624 | vtable = NULL; |
| 625 | } |
| 626 | } else { |
| 627 | vtable = this_object->GetClass()->GetVTable(); |
| 628 | } |
| 629 | if (LIKELY(vtable != NULL && |
| 630 | vtable_index < static_cast<uint32_t>(vtable->GetLength()))) { |
| 631 | return vtable->GetWithoutChecks(vtable_index); |
| 632 | } else { |
Ian Rogers | 08f753d | 2012-08-24 14:35:25 -0700 | [diff] [blame^] | 633 | // Behavior to agree with that of the verifier. |
| 634 | MethodHelper mh(resolved_method); |
| 635 | ThrowNoSuchMethodError(type, resolved_method->GetDeclaringClass(), mh.GetName(), |
| 636 | mh.GetSignature()); |
| 637 | return NULL; // Failure. |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 638 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | } |
Ian Rogers | 60db5ab | 2012-02-20 17:02:00 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 644 | Class* ResolveVerifyAndClinit(uint32_t type_idx, const Method* referrer, Thread* self, |
| 645 | bool can_run_clinit, bool verify_access) { |
| 646 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 647 | Class* klass = class_linker->ResolveType(type_idx, referrer); |
| 648 | if (UNLIKELY(klass == NULL)) { |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 649 | CHECK(self->IsExceptionPending()); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 650 | return NULL; // Failure - Indicate to caller to deliver exception |
jeffhao | 441d912 | 2012-03-21 17:29:10 -0700 | [diff] [blame] | 651 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 652 | // Perform access check if necessary. |
| 653 | Class* referring_class = referrer->GetDeclaringClass(); |
| 654 | if (verify_access && UNLIKELY(!referring_class->CanAccess(klass))) { |
| 655 | ThrowNewIllegalAccessErrorClass(self, referring_class, klass); |
| 656 | return NULL; // Failure - Indicate to caller to deliver exception |
Ian Rogers | 14b1b24 | 2011-10-11 18:54:34 -0700 | [diff] [blame] | 657 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 658 | // If we're just implementing const-class, we shouldn't call <clinit>. |
| 659 | if (!can_run_clinit) { |
| 660 | return klass; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 661 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 662 | // If we are the <clinit> of this class, just return our storage. |
| 663 | // |
| 664 | // Do not set the DexCache InitializedStaticStorage, since that implies <clinit> has finished |
| 665 | // running. |
| 666 | if (klass == referring_class && MethodHelper(referrer).IsClassInitializer()) { |
| 667 | return klass; |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 668 | } |
Ian Rogers | 0045a29 | 2012-03-31 21:08:41 -0700 | [diff] [blame] | 669 | if (!class_linker->EnsureInitialized(klass, true, true)) { |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 670 | CHECK(self->IsExceptionPending()); |
| 671 | return NULL; // Failure - Indicate to caller to deliver exception |
Ian Rogers | dfcdf1a | 2011-10-10 17:50:35 -0700 | [diff] [blame] | 672 | } |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 673 | referrer->GetDexCacheInitializedStaticStorage()->Set(type_idx, klass); |
| 674 | return klass; |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Shih-wei Liao | 2d83101 | 2011-09-28 22:06:53 -0700 | [diff] [blame] | 677 | } // namespace art |