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