blob: de692d1368646a91ccd0972df0882a1bd98e0955 [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"
Mathieu Chartiere401d142015-04-22 13:56:20 -070022#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080023#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070025#include "dex_file-inl.h"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020026#include "dex_instruction-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070027#include "invoke_type.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)
Mathieu Chartier90443472015-07-16 20:32:27 -070037 SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -070049 SHARED_REQUIRES(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)
Mathieu Chartier90443472015-07-16 20:32:27 -070065 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070066 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
Mathieu Chartiere401d142015-04-22 13:56:20 -070081void ThrowAbstractMethodError(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,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 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
Mathieu Chartiere401d142015-04-22 13:56:20 -0700157void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, 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
Mathieu Chartiere401d142015-04-22 13:56:20 -0700171void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700172 std::ostringstream msg;
173 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
174 << PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000175 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800177 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700178}
179
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700180void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800181 va_list args;
182 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000183 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800184 va_end(args);
185}
186
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700187// IllegalAccessException
188
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000189void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700190 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700191}
192
Ian Rogers62d6c772013-02-27 08:32:07 -0800193// IllegalArgumentException
194
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000195void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700196 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800197}
198
199
Ian Rogers87e552d2012-08-31 15:54:48 -0700200// IncompatibleClassChangeError
201
202void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700203 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700204 std::ostringstream msg;
205 msg << "The method '" << PrettyMethod(method) << "' was expected to be of type "
206 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000207 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800209 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700210}
211
Mathieu Chartiere401d142015-04-22 13:56:20 -0700212void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800213 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700215 // Referrer is calling interface_method on this_object, however, the interface_method isn't
216 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700217 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700218 std::ostringstream msg;
219 msg << "Class '" << PrettyDescriptor(this_object->GetClass())
220 << "' does not implement interface '"
221 << PrettyDescriptor(interface_method->GetDeclaringClass())
222 << "' in call to '" << PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000223 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700224 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800225 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700226}
227
Mathieu Chartierc7853442015-03-27 14:35:38 -0700228void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700229 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700230 std::ostringstream msg;
231 msg << "Expected '" << PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700232 << (is_static ? "static" : "instance") << " field" << " rather than a "
233 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700234 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800235 msg.str().c_str());
236}
237
Ian Rogersef7d42f2014-01-06 12:55:46 -0800238void ThrowIncompatibleClassChangeError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800239 va_list args;
240 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000241 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800242 va_end(args);
243}
244
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700245// IOException
246
247void ThrowIOException(const char* fmt, ...) {
248 va_list args;
249 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700250 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700251 va_end(args);
252}
253
Andreas Gampe329d1882014-04-08 10:32:19 -0700254void ThrowWrappedIOException(const char* fmt, ...) {
255 va_list args;
256 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700257 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700258 va_end(args);
259}
260
Ian Rogers62d6c772013-02-27 08:32:07 -0800261// LinkageError
262
Ian Rogersef7d42f2014-01-06 12:55:46 -0800263void ThrowLinkageError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800264 va_list args;
265 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000266 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800267 va_end(args);
268}
269
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100270void ThrowWrappedLinkageError(mirror::Class* referrer, const char* fmt, ...) {
271 va_list args;
272 va_start(args, fmt);
273 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
274 va_end(args);
275}
276
Ian Rogers62d6c772013-02-27 08:32:07 -0800277// NegativeArraySizeException
278
279void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700280 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700281 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800282}
283
284void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700285 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800286}
287
288// NoSuchFieldError
289
290void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700291 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800292 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700293 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800294 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700295 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000296 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700297}
298
Mathieu Chartier4e067782015-05-13 13:13:24 -0700299void ThrowNoSuchFieldException(mirror::Class* c, const StringPiece& name) {
300 std::ostringstream msg;
301 std::string temp;
302 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
303 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
304}
305
Ian Rogers87e552d2012-08-31 15:54:48 -0700306// NoSuchMethodError
307
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800308void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700309 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700310 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700311 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700312 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700313 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000314 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700315}
316
Ian Rogers62d6c772013-02-27 08:32:07 -0800317void ThrowNoSuchMethodError(uint32_t method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700318 ArtMethod* method = Thread::Current()->GetCurrentMethod(nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000319 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700320 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700321 std::ostringstream msg;
322 msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000323 ThrowException("Ljava/lang/NoSuchMethodError;",
324 method->GetDeclaringClass(), msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800325}
326
327// NullPointerException
328
Mathieu Chartierc7853442015-03-27 14:35:38 -0700329void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800330 std::ostringstream msg;
331 msg << "Attempt to " << (is_read ? "read from" : "write to")
332 << " field '" << PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700333 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800334}
335
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000336static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200337 const DexFile& dex_file,
338 InvokeType type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700339 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800340 std::ostringstream msg;
341 msg << "Attempt to invoke " << type << " method '"
342 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700343 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800344}
345
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000346void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200347 InvokeType type) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000348 mirror::DexCache* dex_cache =
349 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200350 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000351 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200352}
353
Mathieu Chartiere401d142015-04-22 13:56:20 -0700354void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200355 InvokeType type) {
356 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
357 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000358 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200359 dex_file, type);
360}
361
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000362void ThrowNullPointerExceptionFromDexPC() {
363 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700364 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000365 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800366 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
367 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Ian Rogers62d6c772013-02-27 08:32:07 -0800368 switch (instr->Opcode()) {
369 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000370 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200371 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800372 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000373 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800374 break;
375 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000376 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200377 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800378 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000379 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800380 break;
381 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000382 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200383 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800384 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000385 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800386 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200387 case Instruction::INVOKE_VIRTUAL_QUICK:
388 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
389 // Since we replaced the method index, we ask the verifier to tell us which
390 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700391 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000392 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700393 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200394 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000395 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200396 } else {
397 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000398 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200399 }
400 break;
401 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800402 case Instruction::IGET:
403 case Instruction::IGET_WIDE:
404 case Instruction::IGET_OBJECT:
405 case Instruction::IGET_BOOLEAN:
406 case Instruction::IGET_BYTE:
407 case Instruction::IGET_CHAR:
408 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700409 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000410 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
411 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800412 break;
413 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200414 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800415 case Instruction::IGET_BOOLEAN_QUICK:
416 case Instruction::IGET_BYTE_QUICK:
417 case Instruction::IGET_CHAR_QUICK:
418 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200419 case Instruction::IGET_WIDE_QUICK:
420 case Instruction::IGET_OBJECT_QUICK: {
421 // Since we replaced the field index, we ask the verifier to tell us which
422 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700423 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000424 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700425 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200426 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000427 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200428 } else {
429 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000430 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200431 }
432 break;
433 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800434 case Instruction::IPUT:
435 case Instruction::IPUT_WIDE:
436 case Instruction::IPUT_OBJECT:
437 case Instruction::IPUT_BOOLEAN:
438 case Instruction::IPUT_BYTE:
439 case Instruction::IPUT_CHAR:
440 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700441 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000442 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
443 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800444 break;
445 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200446 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700447 case Instruction::IPUT_BOOLEAN_QUICK:
448 case Instruction::IPUT_BYTE_QUICK:
449 case Instruction::IPUT_CHAR_QUICK:
450 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200451 case Instruction::IPUT_WIDE_QUICK:
452 case Instruction::IPUT_OBJECT_QUICK: {
453 // Since we replaced the field index, we ask the verifier to tell us which
454 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700455 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000456 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700457 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200458 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000459 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200460 } else {
461 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000462 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200463 }
464 break;
465 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800466 case Instruction::AGET:
467 case Instruction::AGET_WIDE:
468 case Instruction::AGET_OBJECT:
469 case Instruction::AGET_BOOLEAN:
470 case Instruction::AGET_BYTE:
471 case Instruction::AGET_CHAR:
472 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700473 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800474 "Attempt to read from null array");
475 break;
476 case Instruction::APUT:
477 case Instruction::APUT_WIDE:
478 case Instruction::APUT_OBJECT:
479 case Instruction::APUT_BOOLEAN:
480 case Instruction::APUT_BYTE:
481 case Instruction::APUT_CHAR:
482 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700483 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800484 "Attempt to write to null array");
485 break;
486 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700487 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800488 "Attempt to get length of null array");
489 break;
490 default: {
491 // TODO: We should have covered all the cases where we expect a NPE above, this
492 // message/logging is so we can improve any cases we've missed in the future.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000493 const DexFile* dex_file =
494 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700495 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800496 StringPrintf("Null pointer exception during instruction '%s'",
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000497 instr->DumpString(dex_file).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800498 break;
499 }
500 }
501}
502
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000503void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700504 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800505}
506
507// RuntimeException
508
509void ThrowRuntimeException(const char* fmt, ...) {
510 va_list args;
511 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700512 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800513 va_end(args);
514}
515
516// VerifyError
517
Ian Rogersef7d42f2014-01-06 12:55:46 -0800518void ThrowVerifyError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800519 va_list args;
520 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000521 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800522 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700523}
524
525} // namespace art