blob: b401066f7c02ad2310dc148069e13a6161f5fe64 [file] [log] [blame]
Ian Rogers87e552d2012-08-31 15:54:48 -07001/*
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 Rogers22d5e732014-07-15 22:23:51 -070019#include <sstream>
20
Mathieu Chartierc7853442015-03-27 14:35:38 -070021#include "art_field-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080022#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080023#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070024#include "dex_file-inl.h"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020025#include "dex_instruction-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070026#include "invoke_type.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070027#include "mirror/art_method-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070028#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080029#include "mirror/object-inl.h"
30#include "mirror/object_array-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070031#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020032#include "verifier/method_verifier.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070033
Ian Rogers87e552d2012-08-31 15:54:48 -070034namespace art {
35
Ian Rogersef7d42f2014-01-06 12:55:46 -080036static void AddReferrerLocation(std::ostream& os, mirror::Class* referrer)
Ian Rogersb726dcb2012-09-05 08:57:23 -070037 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070038 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070039 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070040 if (!location.empty()) {
41 os << " (declaration of '" << PrettyDescriptor(referrer)
42 << "' appears in " << location << ")";
43 }
44 }
45}
46
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000047static void ThrowException(const char* exception_descriptor,
Mathieu Chartier2cebb242015-04-21 16:50:40 -070048 mirror::Class* referrer, const char* fmt, va_list* args = nullptr)
Ian Rogers62d6c772013-02-27 08:32:07 -080049 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070050 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070051 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080052 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 Geoffray0aa50ce2015-03-10 11:03:29 +000060 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070061}
62
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000063static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier2cebb242015-04-21 16:50:40 -070064 mirror::Class* referrer, const char* fmt, va_list* args = nullptr)
Andreas Gampe329d1882014-04-08 10:32:19 -070065 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
66 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070067 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070068 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 Geoffray0aa50ce2015-03-10 11:03:29 +000076 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070077}
78
Sebastien Hertz56adf602013-07-09 17:27:07 +020079// AbstractMethodError
80
Ian Rogersef7d42f2014-01-06 12:55:46 -080081void ThrowAbstractMethodError(mirror::ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070082 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +020083 StringPrintf("abstract method \"%s\"",
84 PrettyMethod(method).c_str()).c_str());
85}
86
Ian Rogers62d6c772013-02-27 08:32:07 -080087// ArithmeticException
88
Sebastien Hertz0a3b8632013-06-26 11:16:01 +020089void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070090 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -080091}
92
93// ArrayIndexOutOfBoundsException
94
95void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070096 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -080097 StringPrintf("length=%d; index=%d", length, index).c_str());
98}
99
100// ArrayStoreException
101
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102void ThrowArrayStoreException(mirror::Class* element_class, mirror::Class* array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700103 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800104 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 Rogersef7d42f2014-01-06 12:55:46 -0800111void ThrowClassCastException(mirror::Class* dest_type, mirror::Class* src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700112 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800113 StringPrintf("%s cannot be cast to %s",
114 PrettyDescriptor(src_type).c_str(),
115 PrettyDescriptor(dest_type).c_str()).c_str());
116}
117
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000118void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700119 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800120}
121
122// ClassCircularityError
123
124void ThrowClassCircularityError(mirror::Class* c) {
125 std::ostringstream msg;
126 msg << PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000127 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800128}
129
130// ClassFormatError
131
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132void ThrowClassFormatError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800133 va_list args;
134 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000135 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800136 va_end(args);}
137
Ian Rogers87e552d2012-08-31 15:54:48 -0700138// IllegalAccessError
139
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700141 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700142 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700143 << PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000144 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700145}
146
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800147void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800148 mirror::ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700149 InvokeType type) {
150 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700151 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
152 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700153 << " method " << PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000154 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700155}
156
Brian Carlstromea46f952013-07-30 01:26:50 -0700157void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, mirror::ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700158 std::ostringstream msg;
159 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
160 << PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000161 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700162}
163
Mathieu Chartierc7853442015-03-27 14:35:38 -0700164void ThrowIllegalAccessErrorField(mirror::Class* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700165 std::ostringstream msg;
166 msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
167 << PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000168 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700169}
170
Ian Rogersef7d42f2014-01-06 12:55:46 -0800171void ThrowIllegalAccessErrorFinalField(mirror::ArtMethod* referrer,
Mathieu Chartierc7853442015-03-27 14:35:38 -0700172 ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700173 std::ostringstream msg;
174 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
175 << PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000176 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700177 referrer != nullptr ? referrer->GetClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800178 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700179}
180
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700181void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800182 va_list args;
183 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000184 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800185 va_end(args);
186}
187
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700188// IllegalAccessException
189
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000190void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700191 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700192}
193
Ian Rogers62d6c772013-02-27 08:32:07 -0800194// IllegalArgumentException
195
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000196void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700197 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800198}
199
200
Ian Rogers87e552d2012-08-31 15:54:48 -0700201// IncompatibleClassChangeError
202
203void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Brian Carlstromea46f952013-07-30 01:26:50 -0700204 mirror::ArtMethod* method,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800205 mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700206 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 Geoffray0aa50ce2015-03-10 11:03:29 +0000209 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700210 referrer != nullptr ? referrer->GetClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700212}
213
Ian Rogersef7d42f2014-01-06 12:55:46 -0800214void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(mirror::ArtMethod* interface_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 mirror::Object* this_object,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800216 mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700217 // Referrer is calling interface_method on this_object, however, the interface_method isn't
218 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700219 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700220 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 Geoffray0aa50ce2015-03-10 11:03:29 +0000225 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700226 referrer != nullptr ? referrer->GetClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800227 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700228}
229
Mathieu Chartierc7853442015-03-27 14:35:38 -0700230void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Ian Rogersef7d42f2014-01-06 12:55:46 -0800231 mirror::ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700232 std::ostringstream msg;
233 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700234 << (is_static ? "static" : "instance") << " field" << " rather than a "
235 << (is_static ? "instance" : "static") << " field";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000236 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800237 msg.str().c_str());
238}
239
Ian Rogersef7d42f2014-01-06 12:55:46 -0800240void ThrowIncompatibleClassChangeError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800241 va_list args;
242 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000243 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800244 va_end(args);
245}
246
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700247// IOException
248
249void ThrowIOException(const char* fmt, ...) {
250 va_list args;
251 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700252 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700253 va_end(args);
254}
255
Andreas Gampe329d1882014-04-08 10:32:19 -0700256void ThrowWrappedIOException(const char* fmt, ...) {
257 va_list args;
258 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700259 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700260 va_end(args);
261}
262
Ian Rogers62d6c772013-02-27 08:32:07 -0800263// LinkageError
264
Ian Rogersef7d42f2014-01-06 12:55:46 -0800265void ThrowLinkageError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800266 va_list args;
267 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000268 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800269 va_end(args);
270}
271
272// NegativeArraySizeException
273
274void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700275 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700276 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800277}
278
279void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700280 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800281}
282
283// NoSuchFieldError
284
285void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700286 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800287 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700288 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800289 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700290 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000291 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700292}
293
Mathieu Chartier4e067782015-05-13 13:13:24 -0700294void 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 Rogers87e552d2012-08-31 15:54:48 -0700301// NoSuchMethodError
302
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800303void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700304 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700305 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700306 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700307 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700308 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000309 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700310}
311
Ian Rogers62d6c772013-02-27 08:32:07 -0800312void ThrowNoSuchMethodError(uint32_t method_idx) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000313 mirror::ArtMethod* method = Thread::Current()->GetCurrentMethod(nullptr);
314 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700315 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700316 std::ostringstream msg;
317 msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000318 ThrowException("Ljava/lang/NoSuchMethodError;",
319 method->GetDeclaringClass(), msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800320}
321
322// NullPointerException
323
Mathieu Chartierc7853442015-03-27 14:35:38 -0700324void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800325 std::ostringstream msg;
326 msg << "Attempt to " << (is_read ? "read from" : "write to")
327 << " field '" << PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700328 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800329}
330
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000331static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200332 const DexFile& dex_file,
333 InvokeType type)
334 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800335 std::ostringstream msg;
336 msg << "Attempt to invoke " << type << " method '"
337 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700338 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800339}
340
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000341void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200342 InvokeType type) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000343 mirror::DexCache* dex_cache =
344 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200345 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000346 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200347}
348
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000349void ThrowNullPointerExceptionForMethodAccess(mirror::ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200350 InvokeType type) {
351 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
352 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000353 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200354 dex_file, type);
355}
356
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000357void 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 Rogers62d6c772013-02-27 08:32:07 -0800361 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
362 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Ian Rogers62d6c772013-02-27 08:32:07 -0800363 switch (instr->Opcode()) {
364 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000365 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200366 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800367 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000368 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800369 break;
370 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000371 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200372 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800373 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000374 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800375 break;
376 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000377 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200378 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800379 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000380 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800381 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200382 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 Geoffray0aa50ce2015-03-10 11:03:29 +0000386 mirror::ArtMethod* invoked_method =
387 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700388 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200389 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000390 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200391 } else {
392 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000393 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200394 }
395 break;
396 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 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 Chartierc7853442015-03-27 14:35:38 -0700404 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000405 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
406 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800407 break;
408 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200409 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800410 case Instruction::IGET_BOOLEAN_QUICK:
411 case Instruction::IGET_BYTE_QUICK:
412 case Instruction::IGET_CHAR_QUICK:
413 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200414 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 Chartierc7853442015-03-27 14:35:38 -0700418 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000419 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700420 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200421 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000422 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200423 } else {
424 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000425 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200426 }
427 break;
428 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800429 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 Chartierc7853442015-03-27 14:35:38 -0700436 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000437 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
438 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800439 break;
440 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200441 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700442 case Instruction::IPUT_BOOLEAN_QUICK:
443 case Instruction::IPUT_BYTE_QUICK:
444 case Instruction::IPUT_CHAR_QUICK:
445 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200446 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 Chartierc7853442015-03-27 14:35:38 -0700450 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000451 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700452 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200453 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000454 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200455 } else {
456 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000457 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200458 }
459 break;
460 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800461 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 Chartier2cebb242015-04-21 16:50:40 -0700468 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800469 "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 Chartier2cebb242015-04-21 16:50:40 -0700478 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800479 "Attempt to write to null array");
480 break;
481 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700482 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800483 "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 Geoffray0aa50ce2015-03-10 11:03:29 +0000488 const DexFile* dex_file =
489 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700490 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800491 StringPrintf("Null pointer exception during instruction '%s'",
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000492 instr->DumpString(dex_file).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800493 break;
494 }
495 }
496}
497
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000498void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700499 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800500}
501
502// RuntimeException
503
504void ThrowRuntimeException(const char* fmt, ...) {
505 va_list args;
506 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700507 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800508 va_end(args);
509}
510
511// VerifyError
512
Ian Rogersef7d42f2014-01-06 12:55:46 -0800513void ThrowVerifyError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800514 va_list args;
515 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000516 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800517 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700518}
519
520} // namespace art