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