blob: 40e2b1593ec3da0f3e9e8925242d053d971537ec [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
Andreas Gampe103992b2016-01-04 15:32:43 -080021#include "ScopedLocalRef.h"
22
Mathieu Chartierc7853442015-03-27 14:35:38 -070023#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Elliott Hughes07ed66b2012-12-12 18:34:25 -080025#include "base/logging.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "class_linker-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070027#include "dex_file-inl.h"
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +020028#include "dex_instruction-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070029#include "invoke_type.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080031#include "mirror/object-inl.h"
32#include "mirror/object_array-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070033#include "thread.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020034#include "verifier/method_verifier.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070035
Ian Rogers87e552d2012-08-31 15:54:48 -070036namespace art {
37
Ian Rogersef7d42f2014-01-06 12:55:46 -080038static void AddReferrerLocation(std::ostream& os, mirror::Class* referrer)
Mathieu Chartier90443472015-07-16 20:32:27 -070039 SHARED_REQUIRES(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070040 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070041 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070042 if (!location.empty()) {
43 os << " (declaration of '" << PrettyDescriptor(referrer)
44 << "' appears in " << location << ")";
45 }
46 }
47}
48
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000049static void ThrowException(const char* exception_descriptor,
Mathieu Chartier2cebb242015-04-21 16:50:40 -070050 mirror::Class* referrer, const char* fmt, va_list* args = nullptr)
Mathieu Chartier90443472015-07-16 20:32:27 -070051 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070052 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070053 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080054 std::string vmsg;
55 StringAppendV(&vmsg, fmt, *args);
56 msg << vmsg;
57 } else {
58 msg << fmt;
59 }
60 AddReferrerLocation(msg, referrer);
61 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000062 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070063}
64
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000065static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier2cebb242015-04-21 16:50:40 -070066 mirror::Class* referrer, const char* fmt, va_list* args = nullptr)
Mathieu Chartier90443472015-07-16 20:32:27 -070067 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070068 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070069 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070070 std::string vmsg;
71 StringAppendV(&vmsg, fmt, *args);
72 msg << vmsg;
73 } else {
74 msg << fmt;
75 }
76 AddReferrerLocation(msg, referrer);
77 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000078 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070079}
80
Sebastien Hertz56adf602013-07-09 17:27:07 +020081// AbstractMethodError
82
Mathieu Chartiere401d142015-04-22 13:56:20 -070083void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070084 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +020085 StringPrintf("abstract method \"%s\"",
86 PrettyMethod(method).c_str()).c_str());
87}
88
Ian Rogers62d6c772013-02-27 08:32:07 -080089// ArithmeticException
90
Sebastien Hertz0a3b8632013-06-26 11:16:01 +020091void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070092 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -080093}
94
95// ArrayIndexOutOfBoundsException
96
97void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070098 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -080099 StringPrintf("length=%d; index=%d", length, index).c_str());
100}
101
102// ArrayStoreException
103
Ian Rogersef7d42f2014-01-06 12:55:46 -0800104void ThrowArrayStoreException(mirror::Class* element_class, mirror::Class* array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700105 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800106 StringPrintf("%s cannot be stored in an array of type %s",
107 PrettyDescriptor(element_class).c_str(),
108 PrettyDescriptor(array_class).c_str()).c_str());
109}
110
111// ClassCastException
112
Ian Rogersef7d42f2014-01-06 12:55:46 -0800113void ThrowClassCastException(mirror::Class* dest_type, mirror::Class* src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700114 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800115 StringPrintf("%s cannot be cast to %s",
116 PrettyDescriptor(src_type).c_str(),
117 PrettyDescriptor(dest_type).c_str()).c_str());
118}
119
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000120void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700121 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800122}
123
124// ClassCircularityError
125
126void ThrowClassCircularityError(mirror::Class* c) {
127 std::ostringstream msg;
128 msg << PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000129 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800130}
131
132// ClassFormatError
133
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134void ThrowClassFormatError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800135 va_list args;
136 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000137 ThrowException("Ljava/lang/ClassFormatError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800138 va_end(args);}
139
Ian Rogers87e552d2012-08-31 15:54:48 -0700140// IllegalAccessError
141
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800142void ThrowIllegalAccessErrorClass(mirror::Class* referrer, mirror::Class* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700143 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700144 msg << "Illegal class access: '" << PrettyDescriptor(referrer) << "' attempting to access '"
Ian Rogers87e552d2012-08-31 15:54:48 -0700145 << PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000146 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700147}
148
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800149void ThrowIllegalAccessErrorClassForMethodDispatch(mirror::Class* referrer, mirror::Class* accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700150 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700151 InvokeType type) {
152 std::ostringstream msg;
Ian Rogersb726dcb2012-09-05 08:57:23 -0700153 msg << "Illegal class access ('" << PrettyDescriptor(referrer) << "' attempting to access '"
154 << PrettyDescriptor(accessed) << "') in attempt to invoke " << type
Ian Rogers87e552d2012-08-31 15:54:48 -0700155 << " method " << PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000156 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700157}
158
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159void ThrowIllegalAccessErrorMethod(mirror::Class* referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700160 std::ostringstream msg;
161 msg << "Method '" << PrettyMethod(accessed) << "' is inaccessible to class '"
162 << PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000163 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700164}
165
Mathieu Chartierc7853442015-03-27 14:35:38 -0700166void ThrowIllegalAccessErrorField(mirror::Class* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700167 std::ostringstream msg;
168 msg << "Field '" << PrettyField(accessed, false) << "' is inaccessible to class '"
169 << PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000170 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700171}
172
Mathieu Chartiere401d142015-04-22 13:56:20 -0700173void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700174 std::ostringstream msg;
175 msg << "Final field '" << PrettyField(accessed, false) << "' cannot be written to by method '"
176 << PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000177 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700178 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800179 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700180}
181
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700182void ThrowIllegalAccessError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800183 va_list args;
184 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000185 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800186 va_end(args);
187}
188
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700189// IllegalAccessException
190
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000191void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700192 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700193}
194
Ian Rogers62d6c772013-02-27 08:32:07 -0800195// IllegalArgumentException
196
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000197void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700198 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800199}
200
201
Ian Rogers87e552d2012-08-31 15:54:48 -0700202// IncompatibleClassChangeError
203
204void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700205 ArtMethod* method, 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 Chartiere401d142015-04-22 13:56:20 -0700210 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800211 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700212}
213
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800215 mirror::Object* this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700216 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 Chartiere401d142015-04-22 13:56:20 -0700226 referrer != nullptr ? referrer->GetDeclaringClass() : 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,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231 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";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
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
Alex Light9139e002015-10-09 15:59:48 -0700247void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
248 DCHECK(method != nullptr);
249 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
250 /*referrer*/nullptr,
251 StringPrintf("Conflicting default method implementations %s",
252 PrettyMethod(method).c_str()).c_str());
253}
254
255
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700256// IOException
257
258void ThrowIOException(const char* fmt, ...) {
259 va_list args;
260 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700261 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700262 va_end(args);
263}
264
Andreas Gampe329d1882014-04-08 10:32:19 -0700265void ThrowWrappedIOException(const char* fmt, ...) {
266 va_list args;
267 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700268 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700269 va_end(args);
270}
271
Ian Rogers62d6c772013-02-27 08:32:07 -0800272// LinkageError
273
Ian Rogersef7d42f2014-01-06 12:55:46 -0800274void ThrowLinkageError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800275 va_list args;
276 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000277 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800278 va_end(args);
279}
280
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100281void ThrowWrappedLinkageError(mirror::Class* referrer, const char* fmt, ...) {
282 va_list args;
283 va_start(args, fmt);
284 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
285 va_end(args);
286}
287
Ian Rogers62d6c772013-02-27 08:32:07 -0800288// NegativeArraySizeException
289
290void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700291 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700292 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800293}
294
295void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700296 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800297}
298
299// NoSuchFieldError
300
301void ThrowNoSuchFieldError(const StringPiece& scope, mirror::Class* c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700302 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800303 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700304 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800305 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700306 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000307 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700308}
309
Mathieu Chartier4e067782015-05-13 13:13:24 -0700310void ThrowNoSuchFieldException(mirror::Class* c, const StringPiece& name) {
311 std::ostringstream msg;
312 std::string temp;
313 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
314 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
315}
316
Ian Rogers87e552d2012-08-31 15:54:48 -0700317// NoSuchMethodError
318
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800319void ThrowNoSuchMethodError(InvokeType type, mirror::Class* c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700320 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700321 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700322 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700323 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700324 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000325 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700326}
327
Ian Rogers62d6c772013-02-27 08:32:07 -0800328void ThrowNoSuchMethodError(uint32_t method_idx) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700329 ArtMethod* method = Thread::Current()->GetCurrentMethod(nullptr);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000330 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
Ian Rogers4445a7e2012-10-05 17:19:13 -0700331 const DexFile& dex_file = *dex_cache->GetDexFile();
Ian Rogers87e552d2012-08-31 15:54:48 -0700332 std::ostringstream msg;
333 msg << "No method '" << PrettyMethod(method_idx, dex_file, true) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000334 ThrowException("Ljava/lang/NoSuchMethodError;",
335 method->GetDeclaringClass(), msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800336}
337
338// NullPointerException
339
Mathieu Chartierc7853442015-03-27 14:35:38 -0700340void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800341 std::ostringstream msg;
342 msg << "Attempt to " << (is_read ? "read from" : "write to")
343 << " field '" << PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700344 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800345}
346
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000347static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200348 const DexFile& dex_file,
349 InvokeType type)
Mathieu Chartier90443472015-07-16 20:32:27 -0700350 SHARED_REQUIRES(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800351 std::ostringstream msg;
352 msg << "Attempt to invoke " << type << " method '"
353 << PrettyMethod(method_idx, dex_file, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700354 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800355}
356
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000357void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200358 InvokeType type) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000359 mirror::DexCache* dex_cache =
360 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200361 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000362 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200363}
364
Mathieu Chartiere401d142015-04-22 13:56:20 -0700365void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200366 InvokeType type) {
367 mirror::DexCache* dex_cache = method->GetDeclaringClass()->GetDexCache();
368 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000369 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200370 dex_file, type);
371}
372
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000373void ThrowNullPointerExceptionFromDexPC() {
374 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700375 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000376 const DexFile::CodeItem* code = method->GetCodeItem();
Ian Rogers62d6c772013-02-27 08:32:07 -0800377 CHECK_LT(throw_dex_pc, code->insns_size_in_code_units_);
378 const Instruction* instr = Instruction::At(&code->insns_[throw_dex_pc]);
Ian Rogers62d6c772013-02-27 08:32:07 -0800379 switch (instr->Opcode()) {
380 case Instruction::INVOKE_DIRECT:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000381 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200382 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800383 case Instruction::INVOKE_DIRECT_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000384 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800385 break;
386 case Instruction::INVOKE_VIRTUAL:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000387 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200388 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800389 case Instruction::INVOKE_VIRTUAL_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000390 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 break;
392 case Instruction::INVOKE_INTERFACE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000393 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200394 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800395 case Instruction::INVOKE_INTERFACE_RANGE:
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000396 ThrowNullPointerExceptionForMethodAccess(instr->VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200398 case Instruction::INVOKE_VIRTUAL_QUICK:
399 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
400 // Since we replaced the method index, we ask the verifier to tell us which
401 // method is invoked at this location.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 ArtMethod* invoked_method =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000403 verifier::MethodVerifier::FindInvokedMethodAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700404 if (invoked_method != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200405 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000406 ThrowNullPointerExceptionForMethodAccess(invoked_method, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200407 } else {
408 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000409 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200410 }
411 break;
412 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800413 case Instruction::IGET:
414 case Instruction::IGET_WIDE:
415 case Instruction::IGET_OBJECT:
416 case Instruction::IGET_BOOLEAN:
417 case Instruction::IGET_BYTE:
418 case Instruction::IGET_CHAR:
419 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700420 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000421 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
422 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800423 break;
424 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200425 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800426 case Instruction::IGET_BOOLEAN_QUICK:
427 case Instruction::IGET_BYTE_QUICK:
428 case Instruction::IGET_CHAR_QUICK:
429 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200430 case Instruction::IGET_WIDE_QUICK:
431 case Instruction::IGET_OBJECT_QUICK: {
432 // Since we replaced the field index, we ask the verifier to tell us which
433 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700434 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000435 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700436 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200437 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000438 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200439 } else {
440 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000441 ThrowNullPointerException("Attempt to read from a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200442 }
443 break;
444 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800445 case Instruction::IPUT:
446 case Instruction::IPUT_WIDE:
447 case Instruction::IPUT_OBJECT:
448 case Instruction::IPUT_BOOLEAN:
449 case Instruction::IPUT_BYTE:
450 case Instruction::IPUT_CHAR:
451 case Instruction::IPUT_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700452 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000453 Runtime::Current()->GetClassLinker()->ResolveField(instr->VRegC_22c(), method, false);
454 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800455 break;
456 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200457 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700458 case Instruction::IPUT_BOOLEAN_QUICK:
459 case Instruction::IPUT_BYTE_QUICK:
460 case Instruction::IPUT_CHAR_QUICK:
461 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200462 case Instruction::IPUT_WIDE_QUICK:
463 case Instruction::IPUT_OBJECT_QUICK: {
464 // Since we replaced the field index, we ask the verifier to tell us which
465 // field is accessed at this location.
Mathieu Chartierc7853442015-03-27 14:35:38 -0700466 ArtField* field =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000467 verifier::MethodVerifier::FindAccessedFieldAtDexPc(method, throw_dex_pc);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700468 if (field != nullptr) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200469 // NPE with precise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000470 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200471 } else {
472 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000473 ThrowNullPointerException("Attempt to write to a field on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200474 }
475 break;
476 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800477 case Instruction::AGET:
478 case Instruction::AGET_WIDE:
479 case Instruction::AGET_OBJECT:
480 case Instruction::AGET_BOOLEAN:
481 case Instruction::AGET_BYTE:
482 case Instruction::AGET_CHAR:
483 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700484 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800485 "Attempt to read from null array");
486 break;
487 case Instruction::APUT:
488 case Instruction::APUT_WIDE:
489 case Instruction::APUT_OBJECT:
490 case Instruction::APUT_BOOLEAN:
491 case Instruction::APUT_BYTE:
492 case Instruction::APUT_CHAR:
493 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700494 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800495 "Attempt to write to null array");
496 break;
497 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700498 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800499 "Attempt to get length of null array");
500 break;
501 default: {
502 // TODO: We should have covered all the cases where we expect a NPE above, this
503 // message/logging is so we can improve any cases we've missed in the future.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000504 const DexFile* dex_file =
505 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700506 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800507 StringPrintf("Null pointer exception during instruction '%s'",
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000508 instr->DumpString(dex_file).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800509 break;
510 }
511 }
512}
513
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000514void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700515 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800516}
517
518// RuntimeException
519
520void ThrowRuntimeException(const char* fmt, ...) {
521 va_list args;
522 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700523 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800524 va_end(args);
525}
526
Andreas Gampe103992b2016-01-04 15:32:43 -0800527// Stack overflow.
528
529void ThrowStackOverflowError(Thread* self) {
530 if (self->IsHandlingStackOverflow()) {
531 LOG(ERROR) << "Recursive stack overflow.";
532 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
533 }
534
535 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
536 JNIEnvExt* env = self->GetJniEnv();
537 std::string msg("stack size ");
538 msg += PrettySize(self->GetStackSize());
539
540 // Avoid running Java code for exception initialization.
541 // TODO: Checks to make this a bit less brittle.
542
543 std::string error_msg;
544
545 // Allocate an uninitialized object.
546 ScopedLocalRef<jobject> exc(env,
547 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
548 if (exc.get() != nullptr) {
549 // "Initialize".
550 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
551 // Only Throwable has "custom" fields:
552 // String detailMessage.
553 // Throwable cause (= this).
554 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
555 // Object stackState;
556 // StackTraceElement[] stackTrace;
557 // Only Throwable has a non-empty constructor:
558 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
559 // fillInStackTrace();
560
561 // detailMessage.
562 // TODO: Use String::FromModifiedUTF...?
563 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
564 if (s.get() != nullptr) {
565 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
566
567 // cause.
568 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
569
570 // suppressedExceptions.
571 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
572 WellKnownClasses::java_util_Collections,
573 WellKnownClasses::java_util_Collections_EMPTY_LIST));
574 CHECK(emptylist.get() != nullptr);
575 env->SetObjectField(exc.get(),
576 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
577 emptylist.get());
578
579 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
580 // nativeFillInStackTrace.
581 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
582 {
583 ScopedObjectAccessUnchecked soa(env);
584 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
585 }
586 if (stack_state_val.get() != nullptr) {
587 env->SetObjectField(exc.get(),
588 WellKnownClasses::java_lang_Throwable_stackState,
589 stack_state_val.get());
590
591 // stackTrace.
592 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
593 WellKnownClasses::libcore_util_EmptyArray,
594 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
595 env->SetObjectField(exc.get(),
596 WellKnownClasses::java_lang_Throwable_stackTrace,
597 stack_trace_elem.get());
598 } else {
599 error_msg = "Could not create stack trace.";
600 }
601 // Throw the exception.
602 self->SetException(reinterpret_cast<mirror::Throwable*>(self->DecodeJObject(exc.get())));
603 } else {
604 // Could not allocate a string object.
605 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
606 }
607 } else {
608 error_msg = "Could not allocate StackOverflowError object.";
609 }
610
611 if (!error_msg.empty()) {
612 LOG(WARNING) << error_msg;
613 CHECK(self->IsExceptionPending());
614 }
615
616 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
617 self->ResetDefaultStackEnd(); // Return to default stack size.
618
619 // And restore protection if implicit checks are on.
620 if (!explicit_overflow_check) {
621 self->ProtectStack();
622 }
623}
624
Ian Rogers62d6c772013-02-27 08:32:07 -0800625// VerifyError
626
Ian Rogersef7d42f2014-01-06 12:55:46 -0800627void ThrowVerifyError(mirror::Class* referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800628 va_list args;
629 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000630 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800631 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700632}
633
634} // namespace art