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