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