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