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