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