Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 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 "common_throws.h" |
| 18 | |
Elliott Hughes | 07ed66b | 2012-12-12 18:34:25 -0800 | [diff] [blame] | 19 | #include "base/logging.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | #include "class_linker-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 21 | #include "dex_file-inl.h" |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 22 | #include "dex_instruction-inl.h" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 23 | #include "invoke_type.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 24 | #include "mirror/abstract_method-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 25 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 26 | #include "mirror/object-inl.h" |
| 27 | #include "mirror/object_array-inl.h" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 28 | #include "object_utils.h" |
| 29 | #include "thread.h" |
| 30 | |
| 31 | #include <sstream> |
| 32 | |
| 33 | namespace art { |
| 34 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 35 | static void AddReferrerLocation(std::ostream& os, const mirror::Class* referrer) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 36 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 37 | if (referrer != NULL) { |
| 38 | ClassHelper kh(referrer); |
| 39 | std::string location(kh.GetLocation()); |
| 40 | if (!location.empty()) { |
| 41 | os << " (declaration of '" << PrettyDescriptor(referrer) |
| 42 | << "' appears in " << location << ")"; |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 47 | static void ThrowException(const ThrowLocation* throw_location, const char* exception_descriptor, |
| 48 | const mirror::Class* referrer, const char* fmt, va_list* args = NULL) |
| 49 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 50 | std::ostringstream msg; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 51 | if (args != NULL) { |
| 52 | std::string vmsg; |
| 53 | StringAppendV(&vmsg, fmt, *args); |
| 54 | msg << vmsg; |
| 55 | } else { |
| 56 | msg << fmt; |
| 57 | } |
| 58 | AddReferrerLocation(msg, referrer); |
| 59 | Thread* self = Thread::Current(); |
| 60 | if (throw_location == NULL) { |
| 61 | ThrowLocation computed_throw_location = self->GetCurrentLocationForThrow(); |
| 62 | self->ThrowNewException(computed_throw_location, exception_descriptor, msg.str().c_str()); |
| 63 | } else { |
| 64 | self->ThrowNewException(*throw_location, exception_descriptor, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 68 | // ArithmeticException |
| 69 | |
| 70 | void ThrowArithmeticExceptionDivideByZero(Thread* self) { |
| 71 | ThrowException(NULL, "Ljava/lang/ArithmeticException;", NULL, "divide by zero"); |
| 72 | } |
| 73 | |
| 74 | // ArrayIndexOutOfBoundsException |
| 75 | |
| 76 | void ThrowArrayIndexOutOfBoundsException(int index, int length) { |
| 77 | ThrowException(NULL, "Ljava/lang/ArrayIndexOutOfBoundsException;", NULL, |
| 78 | StringPrintf("length=%d; index=%d", length, index).c_str()); |
| 79 | } |
| 80 | |
| 81 | // ArrayStoreException |
| 82 | |
| 83 | void ThrowArrayStoreException(const mirror::Class* element_class, |
| 84 | const mirror::Class* array_class) { |
| 85 | ThrowException(NULL, "Ljava/lang/ArrayStoreException;", NULL, |
| 86 | StringPrintf("%s cannot be stored in an array of type %s", |
| 87 | PrettyDescriptor(element_class).c_str(), |
| 88 | PrettyDescriptor(array_class).c_str()).c_str()); |
| 89 | } |
| 90 | |
| 91 | // ClassCastException |
| 92 | |
| 93 | void ThrowClassCastException(const mirror::Class* dest_type, const mirror::Class* src_type) { |
| 94 | ThrowException(NULL, "Ljava/lang/ClassCastException;", NULL, |
| 95 | StringPrintf("%s cannot be cast to %s", |
| 96 | PrettyDescriptor(src_type).c_str(), |
| 97 | PrettyDescriptor(dest_type).c_str()).c_str()); |
| 98 | } |
| 99 | |
| 100 | void ThrowClassCastException(const ThrowLocation* throw_location, const char* msg) { |
| 101 | ThrowException(throw_location, "Ljava/lang/ClassCastException;", NULL, msg); |
| 102 | } |
| 103 | |
| 104 | // ClassCircularityError |
| 105 | |
| 106 | void ThrowClassCircularityError(mirror::Class* c) { |
| 107 | std::ostringstream msg; |
| 108 | msg << PrettyDescriptor(c); |
| 109 | ThrowException(NULL, "Ljava/lang/ClassCircularityError;", c, msg.str().c_str()); |
| 110 | } |
| 111 | |
| 112 | // ClassFormatError |
| 113 | |
| 114 | void ThrowClassFormatError(const mirror::Class* referrer, const char* fmt, ...) { |
| 115 | va_list args; |
| 116 | va_start(args, fmt); |
| 117 | ThrowException(NULL, "Ljava/lang/ClassFormatError;", referrer, fmt, &args); |
| 118 | va_end(args);} |
| 119 | |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 120 | // IllegalAccessError |
| 121 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 122 | void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 123 | std::ostringstream msg; |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 124 | msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '" |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 125 | << PrettyDescriptor(accessed) << "'"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 126 | ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 129 | void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed, |
| 130 | const mirror::AbstractMethod* caller, |
| 131 | const mirror::AbstractMethod* called, |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 132 | InvokeType type) { |
| 133 | std::ostringstream msg; |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 134 | msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '" |
| 135 | << PrettyDescriptor(accessed) << "') in attempt to invoke " << type |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 136 | << " method " << PrettyMethod(called).c_str(); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 137 | ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 140 | void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, mirror::AbstractMethod* accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 141 | std::ostringstream msg; |
| 142 | msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '" |
| 143 | << PrettyDescriptor(referrer) << "'"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 144 | ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 147 | void ThrowIllegalAccessErrorField(mirror::Class* referrer, mirror::Field* accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 148 | std::ostringstream msg; |
| 149 | msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '" |
| 150 | << PrettyDescriptor(referrer) << "'"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 151 | ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 154 | void ThrowIllegalAccessErrorFinalField(const mirror::AbstractMethod* referrer, |
| 155 | mirror::Field* accessed) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 156 | std::ostringstream msg; |
| 157 | msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '" |
| 158 | << PrettyMethod(referrer) << "'"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 159 | ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer != NULL ? referrer->GetClass() : NULL, |
| 160 | msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 163 | void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...){ |
| 164 | va_list args; |
| 165 | va_start(args, fmt); |
| 166 | ThrowException(NULL, "Ljava/lang/IllegalAccessError;", referrer, fmt, &args); |
| 167 | va_end(args); |
| 168 | } |
| 169 | |
| 170 | // IllegalArgumentException |
| 171 | |
| 172 | void ThrowIllegalArgumentException(const ThrowLocation* throw_location, const char* msg) { |
| 173 | ThrowException(throw_location, "Ljava/lang/IllegalArgumentException;", NULL, msg); |
| 174 | } |
| 175 | |
| 176 | |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 177 | // IncompatibleClassChangeError |
| 178 | |
| 179 | void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type, |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 180 | mirror::AbstractMethod* method, |
| 181 | const mirror::AbstractMethod* referrer) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 182 | std::ostringstream msg; |
| 183 | msg << "The method '" << PrettyMethod(method) << "' was expected to be of type " |
| 184 | << expected_type << " but instead was found to be of type " << found_type; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 185 | ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", |
| 186 | referrer != NULL ? referrer->GetClass() : NULL, |
| 187 | msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 190 | void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(const mirror::AbstractMethod* interface_method, |
| 191 | mirror::Object* this_object, |
| 192 | const mirror::AbstractMethod* referrer) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 193 | // Referrer is calling interface_method on this_object, however, the interface_method isn't |
| 194 | // implemented by this_object. |
| 195 | CHECK(this_object != NULL); |
| 196 | std::ostringstream msg; |
| 197 | msg << "Class '" << PrettyDescriptor(this_object->GetClass()) |
| 198 | << "' does not implement interface '" |
| 199 | << PrettyDescriptor(interface_method->GetDeclaringClass()) |
| 200 | << "' in call to '" << PrettyMethod(interface_method) << "'"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 201 | ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", |
| 202 | referrer != NULL ? referrer->GetClass() : NULL, |
| 203 | msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 206 | void ThrowIncompatibleClassChangeErrorField(const mirror::Field* resolved_field, bool is_static, |
| 207 | const mirror::AbstractMethod* referrer) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 208 | std::ostringstream msg; |
| 209 | msg << "Expected '" << PrettyField(resolved_field) << "' to be a " |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 210 | << (is_static ? "static" : "instance") << " field" << " rather than a " |
| 211 | << (is_static ? "instance" : "static") << " field"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 212 | ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer->GetClass(), |
| 213 | msg.str().c_str()); |
| 214 | } |
| 215 | |
| 216 | void ThrowIncompatibleClassChangeError(const mirror::Class* referrer, const char* fmt, ...){ |
| 217 | va_list args; |
| 218 | va_start(args, fmt); |
| 219 | ThrowException(NULL, "Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args); |
| 220 | va_end(args); |
| 221 | } |
| 222 | |
| 223 | // LinkageError |
| 224 | |
| 225 | void ThrowLinkageError(const mirror::Class* referrer, const char* fmt, ...) { |
| 226 | va_list args; |
| 227 | va_start(args, fmt); |
| 228 | ThrowException(NULL, "Ljava/lang/LinkageError;", referrer, fmt, &args); |
| 229 | va_end(args); |
| 230 | } |
| 231 | |
| 232 | // NegativeArraySizeException |
| 233 | |
| 234 | void ThrowNegativeArraySizeException(int size) { |
| 235 | ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL, StringPrintf("%d", size).c_str()); |
| 236 | } |
| 237 | |
| 238 | void ThrowNegativeArraySizeException(const char* msg) { |
| 239 | ThrowException(NULL, "Ljava/lang/NegativeArraySizeException;", NULL, msg); |
| 240 | } |
| 241 | |
| 242 | // NoSuchFieldError |
| 243 | |
| 244 | void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c, |
| 245 | const StringPiece& type, const StringPiece& name) |
| 246 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 247 | ClassHelper kh(c); |
| 248 | std::ostringstream msg; |
| 249 | msg << "No " << scope << "field " << name << " of type " << type |
| 250 | << " in class " << kh.GetDescriptor() << " or its superclasses"; |
| 251 | ThrowException(NULL, "Ljava/lang/NoSuchFieldError;", c, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // NoSuchMethodError |
| 255 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 256 | void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 257 | const StringPiece& signature) { |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 258 | std::ostringstream msg; |
| 259 | ClassHelper kh(c); |
| 260 | msg << "No " << type << " method " << name << signature |
| 261 | << " in class " << kh.GetDescriptor() << " or its super classes"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 262 | ThrowException(NULL, "Ljava/lang/NoSuchMethodError;", c, msg.str().c_str()); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 265 | void ThrowNoSuchMethodError(uint32_t method_idx) { |
| 266 | Thread* self = Thread::Current(); |
| 267 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 268 | mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache(); |
Ian Rogers | 4445a7e | 2012-10-05 17:19:13 -0700 | [diff] [blame] | 269 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 270 | std::ostringstream msg; |
| 271 | msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 272 | ThrowException(&throw_location, "Ljava/lang/NoSuchMethodError;", |
| 273 | throw_location.GetMethod()->GetDeclaringClass(), msg.str().c_str()); |
| 274 | } |
| 275 | |
| 276 | // NullPointerException |
| 277 | |
| 278 | void ThrowNullPointerExceptionForFieldAccess(const ThrowLocation& throw_location, |
| 279 | mirror::Field* field, bool is_read) { |
| 280 | std::ostringstream msg; |
| 281 | msg << "Attempt to " << (is_read ? "read from" : "write to") |
| 282 | << " field '" << PrettyField(field, true) << "' on a null object reference"; |
| 283 | ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str()); |
| 284 | } |
| 285 | |
| 286 | void ThrowNullPointerExceptionForMethodAccess(const ThrowLocation& throw_location, uint32_t method_idx, |
| 287 | InvokeType type) { |
| 288 | mirror::DexCache* dex_cache = throw_location.GetMethod()->GetDeclaringClass()->GetDexCache(); |
| 289 | const DexFile& dex_file = *dex_cache->GetDexFile(); |
| 290 | std::ostringstream msg; |
| 291 | msg << "Attempt to invoke " << type << " method '" |
| 292 | << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference"; |
| 293 | ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, msg.str().c_str()); |
| 294 | } |
| 295 | |
| 296 | void ThrowNullPointerExceptionFromDexPC(const ThrowLocation& throw_location) { |
| 297 | const DexFile::CodeItem* code = MethodHelper(throw_location.GetMethod()).GetCodeItem(); |
| 298 | uint32_t throw_dex_pc = throw_location.GetDexPc(); |
| 299 | CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_); |
| 300 | const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 301 | switch (instr->Opcode()) { |
| 302 | case Instruction::INVOKE_DIRECT: |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 303 | ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kDirect); |
| 304 | break; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 305 | case Instruction::INVOKE_DIRECT_RANGE: |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 306 | ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kDirect); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 307 | break; |
| 308 | case Instruction::INVOKE_VIRTUAL: |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 309 | ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kVirtual); |
| 310 | break; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 311 | case Instruction::INVOKE_VIRTUAL_RANGE: |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 312 | ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kVirtual); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 313 | break; |
| 314 | case Instruction::INVOKE_INTERFACE: |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 315 | ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_35c(), kInterface); |
| 316 | break; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 317 | case Instruction::INVOKE_INTERFACE_RANGE: |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 318 | ThrowNullPointerExceptionForMethodAccess(throw_location, instr->VRegB_3rc(), kInterface); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 319 | break; |
| 320 | case Instruction::IGET: |
| 321 | case Instruction::IGET_WIDE: |
| 322 | case Instruction::IGET_OBJECT: |
| 323 | case Instruction::IGET_BOOLEAN: |
| 324 | case Instruction::IGET_BYTE: |
| 325 | case Instruction::IGET_CHAR: |
| 326 | case Instruction::IGET_SHORT: { |
| 327 | mirror::Field* field = |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 328 | Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 329 | throw_location.GetMethod(), false); |
| 330 | ThrowNullPointerExceptionForFieldAccess(throw_location, field, true /* read */); |
| 331 | break; |
| 332 | } |
| 333 | case Instruction::IPUT: |
| 334 | case Instruction::IPUT_WIDE: |
| 335 | case Instruction::IPUT_OBJECT: |
| 336 | case Instruction::IPUT_BOOLEAN: |
| 337 | case Instruction::IPUT_BYTE: |
| 338 | case Instruction::IPUT_CHAR: |
| 339 | case Instruction::IPUT_SHORT: { |
| 340 | mirror::Field* field = |
Sebastien Hertz | 75b2a4a | 2013-05-21 09:25:10 +0200 | [diff] [blame] | 341 | Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 342 | throw_location.GetMethod(), false); |
| 343 | ThrowNullPointerExceptionForFieldAccess(throw_location, field, false /* write */); |
| 344 | break; |
| 345 | } |
| 346 | case Instruction::AGET: |
| 347 | case Instruction::AGET_WIDE: |
| 348 | case Instruction::AGET_OBJECT: |
| 349 | case Instruction::AGET_BOOLEAN: |
| 350 | case Instruction::AGET_BYTE: |
| 351 | case Instruction::AGET_CHAR: |
| 352 | case Instruction::AGET_SHORT: |
| 353 | ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, |
| 354 | "Attempt to read from null array"); |
| 355 | break; |
| 356 | case Instruction::APUT: |
| 357 | case Instruction::APUT_WIDE: |
| 358 | case Instruction::APUT_OBJECT: |
| 359 | case Instruction::APUT_BOOLEAN: |
| 360 | case Instruction::APUT_BYTE: |
| 361 | case Instruction::APUT_CHAR: |
| 362 | case Instruction::APUT_SHORT: |
| 363 | ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, |
| 364 | "Attempt to write to null array"); |
| 365 | break; |
| 366 | case Instruction::ARRAY_LENGTH: |
| 367 | ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, |
| 368 | "Attempt to get length of null array"); |
| 369 | break; |
| 370 | default: { |
| 371 | // TODO: We should have covered all the cases where we expect a NPE above, this |
| 372 | // message/logging is so we can improve any cases we've missed in the future. |
| 373 | const DexFile& dex_file = |
| 374 | *throw_location.GetMethod()->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
| 375 | ThrowException(&throw_location, "Ljava/lang/NullPointerException;", NULL, |
| 376 | StringPrintf("Null pointer exception during instruction '%s'", |
| 377 | instr->DumpString(&dex_file).c_str()).c_str()); |
| 378 | break; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | void ThrowNullPointerException(const ThrowLocation* throw_location, const char* msg) { |
| 384 | ThrowException(throw_location, "Ljava/lang/NullPointerException;", NULL, msg); |
| 385 | } |
| 386 | |
| 387 | // RuntimeException |
| 388 | |
| 389 | void ThrowRuntimeException(const char* fmt, ...) { |
| 390 | va_list args; |
| 391 | va_start(args, fmt); |
| 392 | ThrowException(NULL, "Ljava/lang/RuntimeException;", NULL, fmt, &args); |
| 393 | va_end(args); |
| 394 | } |
| 395 | |
| 396 | // VerifyError |
| 397 | |
| 398 | void ThrowVerifyError(const mirror::Class* referrer, const char* fmt, ...) { |
| 399 | va_list args; |
| 400 | va_start(args, fmt); |
| 401 | ThrowException(NULL, "Ljava/lang/VerifyError;", referrer, fmt, &args); |
| 402 | va_end(args); |
Ian Rogers | 87e552d | 2012-08-31 15:54:48 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | } // namespace art |