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