blob: 945442d539bbf07c54776e2995e98b310da1829c [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 Gampe57943812017-12-06 21:39:13 -080021#include <android-base/logging.h>
22#include <android-base/stringprintf.h>
Andreas Gampe103992b2016-01-04 15:32:43 -080023
Mathieu Chartierc7853442015-03-27 14:35:38 -070024#include "art_field-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070025#include "art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080026#include "class_linker-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080027#include "dex/dex_file-inl.h"
28#include "dex/dex_instruction-inl.h"
David Sehr8c0961f2018-01-23 16:11:38 -080029#include "dex/invoke_type.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070030#include "mirror/class-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010031#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080032#include "mirror/object-inl.h"
33#include "mirror/object_array-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070034#include "nativehelper/scoped_local_ref.h"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070035#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070036#include "thread.h"
Nicolas Geoffrayb041a402017-11-13 15:16:22 +000037#include "vdex_file.h"
Sebastien Hertz2d6ba512013-05-17 11:31:37 +020038#include "verifier/method_verifier.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070039#include "well_known_classes.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070040
Ian Rogers87e552d2012-08-31 15:54:48 -070041namespace art {
42
Andreas Gampe46ee31b2016-12-14 10:11:49 -080043using android::base::StringAppendV;
44using android::base::StringPrintf;
45
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070046static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070047 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070048 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070049 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070050 if (!location.empty()) {
David Sehr709b0702016-10-13 09:12:37 -070051 os << " (declaration of '" << referrer->PrettyDescriptor()
52 << "' appears in " << location << ")";
Ian Rogers87e552d2012-08-31 15:54:48 -070053 }
54 }
55}
56
Orion Hodson928033d2018-02-07 05:30:54 +000057static void ThrowException(const char* exception_descriptor) REQUIRES_SHARED(Locks::mutator_lock_) {
58 Thread* self = Thread::Current();
59 self->ThrowNewException(exception_descriptor, nullptr);
60}
61
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000062static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070063 ObjPtr<mirror::Class> referrer,
64 const char* fmt,
65 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070066 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070067 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070068 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080069 std::string vmsg;
70 StringAppendV(&vmsg, fmt, *args);
71 msg << vmsg;
72 } else {
73 msg << fmt;
74 }
75 AddReferrerLocation(msg, referrer);
76 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000077 self->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070078}
79
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000080static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070081 ObjPtr<mirror::Class> referrer,
82 const char* fmt,
83 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070084 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070085 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070086 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070087 std::string vmsg;
88 StringAppendV(&vmsg, fmt, *args);
89 msg << vmsg;
90 } else {
91 msg << fmt;
92 }
93 AddReferrerLocation(msg, referrer);
94 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000095 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070096}
97
Sebastien Hertz56adf602013-07-09 17:27:07 +020098// AbstractMethodError
99
Mathieu Chartiere401d142015-04-22 13:56:20 -0700100void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700101 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +0200102 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700103 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +0200104}
105
Alex Light705ad492015-09-21 11:36:30 -0700106void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
107 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer */ nullptr,
108 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700109 dex_file.PrettyMethod(method_idx,
110 /* with_signature */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700111}
112
Ian Rogers62d6c772013-02-27 08:32:07 -0800113// ArithmeticException
114
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200115void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700116 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800117}
118
119// ArrayIndexOutOfBoundsException
120
121void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700122 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800123 StringPrintf("length=%d; index=%d", length, index).c_str());
124}
125
126// ArrayStoreException
127
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700128void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
129 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700130 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800131 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700132 mirror::Class::PrettyDescriptor(element_class).c_str(),
133 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800134}
135
Orion Hodsonc069a302017-01-18 09:23:12 +0000136// BootstrapMethodError
137
138void ThrowBootstrapMethodError(const char* fmt, ...) {
139 va_list args;
140 va_start(args, fmt);
141 ThrowException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
142 va_end(args);
143}
144
145void ThrowWrappedBootstrapMethodError(const char* fmt, ...) {
146 va_list args;
147 va_start(args, fmt);
148 ThrowWrappedException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
149 va_end(args);
150}
151
Ian Rogers62d6c772013-02-27 08:32:07 -0800152// ClassCastException
153
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700154void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700155 ThrowException("Ljava/lang/ClassCastException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800156 StringPrintf("%s cannot be cast to %s",
David Sehr709b0702016-10-13 09:12:37 -0700157 mirror::Class::PrettyDescriptor(src_type).c_str(),
158 mirror::Class::PrettyDescriptor(dest_type).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800159}
160
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000161void ThrowClassCastException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700162 ThrowException("Ljava/lang/ClassCastException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800163}
164
165// ClassCircularityError
166
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700167void ThrowClassCircularityError(ObjPtr<mirror::Class> c) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800168 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700169 msg << mirror::Class::PrettyDescriptor(c);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000170 ThrowException("Ljava/lang/ClassCircularityError;", c, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800171}
172
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700173void ThrowClassCircularityError(ObjPtr<mirror::Class> c, const char* fmt, ...) {
Roland Levillain989ab3b2016-05-18 15:52:54 +0100174 va_list args;
175 va_start(args, fmt);
176 ThrowException("Ljava/lang/ClassCircularityError;", c, fmt, &args);
177 va_end(args);
178}
179
Ian Rogers62d6c772013-02-27 08:32:07 -0800180// ClassFormatError
181
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700182void ThrowClassFormatError(ObjPtr<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/ClassFormatError;", referrer, fmt, &args);
Roland Levillainab880f42016-05-12 16:24:36 +0100186 va_end(args);
187}
Ian Rogers62d6c772013-02-27 08:32:07 -0800188
Ian Rogers87e552d2012-08-31 15:54:48 -0700189// IllegalAccessError
190
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700191void ThrowIllegalAccessErrorClass(ObjPtr<mirror::Class> referrer, ObjPtr<mirror::Class> accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700192 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700193 msg << "Illegal class access: '" << mirror::Class::PrettyDescriptor(referrer)
194 << "' attempting to access '" << mirror::Class::PrettyDescriptor(accessed) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000195 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700196}
197
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700198void ThrowIllegalAccessErrorClassForMethodDispatch(ObjPtr<mirror::Class> referrer,
199 ObjPtr<mirror::Class> accessed,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700200 ArtMethod* called,
Ian Rogers87e552d2012-08-31 15:54:48 -0700201 InvokeType type) {
202 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700203 msg << "Illegal class access ('" << mirror::Class::PrettyDescriptor(referrer)
204 << "' attempting to access '"
205 << mirror::Class::PrettyDescriptor(accessed) << "') in attempt to invoke " << type
206 << " method " << ArtMethod::PrettyMethod(called).c_str();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000207 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700208}
209
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700210void ThrowIllegalAccessErrorMethod(ObjPtr<mirror::Class> referrer, ArtMethod* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700211 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700212 msg << "Method '" << ArtMethod::PrettyMethod(accessed) << "' is inaccessible to class '"
213 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000214 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700215}
216
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700217void ThrowIllegalAccessErrorField(ObjPtr<mirror::Class> referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700218 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700219 msg << "Field '" << ArtField::PrettyField(accessed, false) << "' is inaccessible to class '"
220 << mirror::Class::PrettyDescriptor(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000221 ThrowException("Ljava/lang/IllegalAccessError;", referrer, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700222}
223
Mathieu Chartiere401d142015-04-22 13:56:20 -0700224void ThrowIllegalAccessErrorFinalField(ArtMethod* referrer, ArtField* accessed) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700225 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700226 msg << "Final field '" << ArtField::PrettyField(accessed, false)
227 << "' cannot be written to by method '" << ArtMethod::PrettyMethod(referrer) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000228 ThrowException("Ljava/lang/IllegalAccessError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700229 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800230 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700231}
232
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700233void ThrowIllegalAccessError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800234 va_list args;
235 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000236 ThrowException("Ljava/lang/IllegalAccessError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800237 va_end(args);
238}
239
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700240// IllegalAccessException
241
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000242void ThrowIllegalAccessException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700243 ThrowException("Ljava/lang/IllegalAccessException;", nullptr, msg);
Jeff Hao11d5d8f2014-03-26 15:08:20 -0700244}
245
Ian Rogers62d6c772013-02-27 08:32:07 -0800246// IllegalArgumentException
247
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000248void ThrowIllegalArgumentException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700249 ThrowException("Ljava/lang/IllegalArgumentException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800250}
251
Orion Hodson928033d2018-02-07 05:30:54 +0000252// IllegalStateException
253
254void ThrowIllegalStateException(const char* msg) {
255 ThrowException("Ljava/lang/IllegalStateException;", nullptr, msg);
256}
Ian Rogers62d6c772013-02-27 08:32:07 -0800257
Ian Rogers87e552d2012-08-31 15:54:48 -0700258// IncompatibleClassChangeError
259
260void ThrowIncompatibleClassChangeError(InvokeType expected_type, InvokeType found_type,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 ArtMethod* method, ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700262 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700263 msg << "The method '" << ArtMethod::PrettyMethod(method) << "' was expected to be of type "
Ian Rogers87e552d2012-08-31 15:54:48 -0700264 << expected_type << " but instead was found to be of type " << found_type;
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000265 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700266 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800267 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700268}
269
Alex Light705ad492015-09-21 11:36:30 -0700270void ThrowIncompatibleClassChangeErrorClassForInterfaceSuper(ArtMethod* method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700271 ObjPtr<mirror::Class> target_class,
272 ObjPtr<mirror::Object> this_object,
Alex Light705ad492015-09-21 11:36:30 -0700273 ArtMethod* referrer) {
274 // Referrer is calling interface_method on this_object, however, the interface_method isn't
275 // implemented by this_object.
276 CHECK(this_object != nullptr);
277 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700278 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
279 << "' does not implement interface '" << mirror::Class::PrettyDescriptor(target_class)
280 << "' in call to '"
281 << ArtMethod::PrettyMethod(method) << "'";
Alex Light705ad492015-09-21 11:36:30 -0700282 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
283 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
284 msg.str().c_str());
285}
286
Mathieu Chartiere401d142015-04-22 13:56:20 -0700287void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700288 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700289 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700290 // Referrer is calling interface_method on this_object, however, the interface_method isn't
291 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700292 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700293 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700294 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
Ian Rogers87e552d2012-08-31 15:54:48 -0700295 << "' does not implement interface '"
David Sehr709b0702016-10-13 09:12:37 -0700296 << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
297 << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000298 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800300 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700301}
302
Mathieu Chartierc7853442015-03-27 14:35:38 -0700303void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700304 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700305 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700306 msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700307 << (is_static ? "static" : "instance") << " field" << " rather than a "
308 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700309 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800310 msg.str().c_str());
311}
312
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700313void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800314 va_list args;
315 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000316 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800317 va_end(args);
318}
319
Alex Light9139e002015-10-09 15:59:48 -0700320void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
321 DCHECK(method != nullptr);
322 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
323 /*referrer*/nullptr,
324 StringPrintf("Conflicting default method implementations %s",
David Sehr709b0702016-10-13 09:12:37 -0700325 ArtMethod::PrettyMethod(method).c_str()).c_str());
Alex Light9139e002015-10-09 15:59:48 -0700326}
327
Orion Hodson928033d2018-02-07 05:30:54 +0000328// IndexOutOfBoundsException
329
330void ThrowIndexOutOfBoundsException(int index, int length) {
331 ThrowException("Ljava/lang/IndexOutOfBoundsException;", nullptr,
332 StringPrintf("length=%d; index=%d", length, index).c_str());
333}
334
Alex Lightdb01a092017-04-03 15:39:55 -0700335// InternalError
336
337void ThrowInternalError(const char* fmt, ...) {
338 va_list args;
339 va_start(args, fmt);
340 ThrowException("Ljava/lang/InternalError;", nullptr, fmt, &args);
341 va_end(args);
342}
Alex Light9139e002015-10-09 15:59:48 -0700343
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700344// IOException
345
346void ThrowIOException(const char* fmt, ...) {
347 va_list args;
348 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700349 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700350 va_end(args);
351}
352
Andreas Gampe329d1882014-04-08 10:32:19 -0700353void ThrowWrappedIOException(const char* fmt, ...) {
354 va_list args;
355 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700356 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700357 va_end(args);
358}
359
Ian Rogers62d6c772013-02-27 08:32:07 -0800360// LinkageError
361
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700362void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800363 va_list args;
364 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000365 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800366 va_end(args);
367}
368
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700369void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100370 va_list args;
371 va_start(args, fmt);
372 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
373 va_end(args);
374}
375
Ian Rogers62d6c772013-02-27 08:32:07 -0800376// NegativeArraySizeException
377
378void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700379 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700380 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800381}
382
383void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700384 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800385}
386
387// NoSuchFieldError
388
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700389void ThrowNoSuchFieldError(const StringPiece& scope, ObjPtr<mirror::Class> c,
Mathieu Chartier4e067782015-05-13 13:13:24 -0700390 const StringPiece& type, const StringPiece& name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800391 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700392 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800393 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700394 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000395 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700396}
397
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700398void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, const StringPiece& name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700399 std::ostringstream msg;
400 std::string temp;
401 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
402 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
403}
404
Ian Rogers87e552d2012-08-31 15:54:48 -0700405// NoSuchMethodError
406
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700407void ThrowNoSuchMethodError(InvokeType type, ObjPtr<mirror::Class> c, const StringPiece& name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700408 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700409 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700410 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700411 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700412 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000413 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700414}
415
Ian Rogers62d6c772013-02-27 08:32:07 -0800416// NullPointerException
417
Mathieu Chartierc7853442015-03-27 14:35:38 -0700418void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800419 std::ostringstream msg;
420 msg << "Attempt to " << (is_read ? "read from" : "write to")
David Sehr709b0702016-10-13 09:12:37 -0700421 << " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700422 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800423}
424
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000425static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200426 const DexFile& dex_file,
427 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700428 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800429 std::ostringstream msg;
430 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700431 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700432 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800433}
434
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000435void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200436 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700437 ObjPtr<mirror::DexCache> dex_cache =
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000438 Thread::Current()->GetCurrentMethod(nullptr)->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200439 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000440 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200441}
442
Mathieu Chartiere401d142015-04-22 13:56:20 -0700443void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200444 InvokeType type) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700445 ObjPtr<mirror::DexCache> dex_cache = method->GetDeclaringClass()->GetDexCache();
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200446 const DexFile& dex_file = *dex_cache->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000447 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200448 dex_file, type);
449}
450
Vladimir Marko953437b2016-08-24 08:30:46 +0000451static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
452 DCHECK(kEmitCompilerReadBarrier);
453 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
Vladimir Marko33bff252017-11-01 14:35:42 +0000454 if (kUseBakerReadBarrier &&
455 (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64)) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000456 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
457 monitor_offset += gray_byte_position;
458 }
459 return addr == monitor_offset;
460}
461
Nicolas Geoffray13449142017-12-07 22:26:24 +0000462static bool IsValidImplicitCheck(uintptr_t addr, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700463 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100464 if (!CanDoImplicitNullCheckOn(addr)) {
465 return false;
466 }
467
468 switch (instr.Opcode()) {
469 case Instruction::INVOKE_DIRECT:
470 case Instruction::INVOKE_DIRECT_RANGE:
471 case Instruction::INVOKE_VIRTUAL:
472 case Instruction::INVOKE_VIRTUAL_RANGE:
473 case Instruction::INVOKE_INTERFACE:
474 case Instruction::INVOKE_INTERFACE_RANGE:
Orion Hodsonac141392017-01-13 11:53:47 +0000475 case Instruction::INVOKE_POLYMORPHIC:
476 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100477 case Instruction::INVOKE_VIRTUAL_QUICK:
478 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
479 // Without inlining, we could just check that the offset is the class offset.
480 // However, when inlining, the compiler can (validly) merge the null check with a field access
481 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
482 // which is the caller.
483 return true;
484 }
485
Vladimir Marko953437b2016-08-24 08:30:46 +0000486 case Instruction::IGET_OBJECT:
487 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
488 return true;
489 }
490 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100491 case Instruction::IGET:
492 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100493 case Instruction::IGET_BOOLEAN:
494 case Instruction::IGET_BYTE:
495 case Instruction::IGET_CHAR:
496 case Instruction::IGET_SHORT:
497 case Instruction::IPUT:
498 case Instruction::IPUT_WIDE:
499 case Instruction::IPUT_OBJECT:
500 case Instruction::IPUT_BOOLEAN:
501 case Instruction::IPUT_BYTE:
502 case Instruction::IPUT_CHAR:
503 case Instruction::IPUT_SHORT: {
Nicolas Geoffray13449142017-12-07 22:26:24 +0000504 // We might be doing an implicit null check with an offset that doesn't correspond
505 // to the instruction, for example with two field accesses and the first one being
506 // eliminated or re-ordered.
507 return true;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100508 }
509
Vladimir Marko953437b2016-08-24 08:30:46 +0000510 case Instruction::IGET_OBJECT_QUICK:
511 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
512 return true;
513 }
514 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100515 case Instruction::IGET_QUICK:
516 case Instruction::IGET_BOOLEAN_QUICK:
517 case Instruction::IGET_BYTE_QUICK:
518 case Instruction::IGET_CHAR_QUICK:
519 case Instruction::IGET_SHORT_QUICK:
520 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100521 case Instruction::IPUT_QUICK:
522 case Instruction::IPUT_BOOLEAN_QUICK:
523 case Instruction::IPUT_BYTE_QUICK:
524 case Instruction::IPUT_CHAR_QUICK:
525 case Instruction::IPUT_SHORT_QUICK:
526 case Instruction::IPUT_WIDE_QUICK:
527 case Instruction::IPUT_OBJECT_QUICK: {
Nicolas Geoffray13449142017-12-07 22:26:24 +0000528 // We might be doing an implicit null check with an offset that doesn't correspond
529 // to the instruction, for example with two field accesses and the first one being
530 // eliminated or re-ordered.
531 return true;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100532 }
533
Vladimir Marko953437b2016-08-24 08:30:46 +0000534 case Instruction::AGET_OBJECT:
535 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
536 return true;
537 }
538 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100539 case Instruction::AGET:
540 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100541 case Instruction::AGET_BOOLEAN:
542 case Instruction::AGET_BYTE:
543 case Instruction::AGET_CHAR:
544 case Instruction::AGET_SHORT:
545 case Instruction::APUT:
546 case Instruction::APUT_WIDE:
547 case Instruction::APUT_OBJECT:
548 case Instruction::APUT_BOOLEAN:
549 case Instruction::APUT_BYTE:
550 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100551 case Instruction::APUT_SHORT:
552 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100553 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100554 // The length access should crash. We currently do not do implicit checks on
555 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000556 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100557 }
558
559 default: {
560 // We have covered all the cases where an NPE could occur.
561 // Note that this must be kept in sync with the compiler, and adding
562 // any new way to do implicit checks in the compiler should also update
563 // this code.
564 return false;
565 }
566 }
567}
568
569void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000570 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700571 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
David Sehr0225f8e2018-01-31 08:52:24 +0000572 CodeItemInstructionAccessor accessor(method->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800573 CHECK_LT(throw_dex_pc, accessor.InsnsSizeInCodeUnits());
574 const Instruction& instr = accessor.InstructionAt(throw_dex_pc);
575 if (check_address && !IsValidImplicitCheck(addr, instr)) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100576 const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile();
577 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
578 << "0x" << std::hex << addr << std::dec
579 << ", at "
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800580 << instr.DumpString(dex_file)
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100581 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700582 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100583 }
584
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800585 switch (instr.Opcode()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800586 case Instruction::INVOKE_DIRECT:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800587 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200588 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800589 case Instruction::INVOKE_DIRECT_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800590 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800591 break;
592 case Instruction::INVOKE_VIRTUAL:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800593 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200594 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800595 case Instruction::INVOKE_VIRTUAL_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800596 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800597 break;
598 case Instruction::INVOKE_INTERFACE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800599 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200600 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800601 case Instruction::INVOKE_INTERFACE_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800602 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800603 break;
Orion Hodsonac141392017-01-13 11:53:47 +0000604 case Instruction::INVOKE_POLYMORPHIC:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800605 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_45cc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000606 break;
607 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800608 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_4rcc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000609 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200610 case Instruction::INVOKE_VIRTUAL_QUICK:
611 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000612 uint16_t method_idx = method->GetIndexFromQuickening(throw_dex_pc);
613 if (method_idx != DexFile::kDexNoIndex16) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200614 // NPE with precise message.
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000615 ThrowNullPointerExceptionForMethodAccess(method_idx, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200616 } else {
617 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000618 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200619 }
620 break;
621 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800622 case Instruction::IGET:
623 case Instruction::IGET_WIDE:
624 case Instruction::IGET_OBJECT:
625 case Instruction::IGET_BOOLEAN:
626 case Instruction::IGET_BYTE:
627 case Instruction::IGET_CHAR:
628 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700629 ArtField* field =
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800630 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Andreas Gampe71307442018-02-06 13:38:03 -0800631 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000632 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800633 break;
634 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200635 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800636 case Instruction::IGET_BOOLEAN_QUICK:
637 case Instruction::IGET_BYTE_QUICK:
638 case Instruction::IGET_CHAR_QUICK:
639 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200640 case Instruction::IGET_WIDE_QUICK:
641 case Instruction::IGET_OBJECT_QUICK: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000642 uint16_t field_idx = method->GetIndexFromQuickening(throw_dex_pc);
643 ArtField* field = nullptr;
644 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
645 field = Runtime::Current()->GetClassLinker()->ResolveField(
646 field_idx, method, /* is_static */ false);
647 Thread::Current()->ClearException(); // Resolution may fail, ignore.
648 ThrowNullPointerExceptionForFieldAccess(field, true /* read */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200649 break;
650 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800651 case Instruction::IPUT:
652 case Instruction::IPUT_WIDE:
653 case Instruction::IPUT_OBJECT:
654 case Instruction::IPUT_BOOLEAN:
655 case Instruction::IPUT_BYTE:
656 case Instruction::IPUT_CHAR:
657 case Instruction::IPUT_SHORT: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000658 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveField(
659 instr.VRegC_22c(), method, /* is_static */ false);
Andreas Gampe71307442018-02-06 13:38:03 -0800660 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000661 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Ian Rogers62d6c772013-02-27 08:32:07 -0800662 break;
663 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200664 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700665 case Instruction::IPUT_BOOLEAN_QUICK:
666 case Instruction::IPUT_BYTE_QUICK:
667 case Instruction::IPUT_CHAR_QUICK:
668 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200669 case Instruction::IPUT_WIDE_QUICK:
670 case Instruction::IPUT_OBJECT_QUICK: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000671 uint16_t field_idx = method->GetIndexFromQuickening(throw_dex_pc);
672 ArtField* field = nullptr;
673 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
674 field = Runtime::Current()->GetClassLinker()->ResolveField(
675 field_idx, method, /* is_static */ false);
676 Thread::Current()->ClearException(); // Resolution may fail, ignore.
677 ThrowNullPointerExceptionForFieldAccess(field, false /* write */);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200678 break;
679 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800680 case Instruction::AGET:
681 case Instruction::AGET_WIDE:
682 case Instruction::AGET_OBJECT:
683 case Instruction::AGET_BOOLEAN:
684 case Instruction::AGET_BYTE:
685 case Instruction::AGET_CHAR:
686 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700687 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800688 "Attempt to read from null array");
689 break;
690 case Instruction::APUT:
691 case Instruction::APUT_WIDE:
692 case Instruction::APUT_OBJECT:
693 case Instruction::APUT_BOOLEAN:
694 case Instruction::APUT_BYTE:
695 case Instruction::APUT_CHAR:
696 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700697 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800698 "Attempt to write to null array");
699 break;
700 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700701 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800702 "Attempt to get length of null array");
703 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100704 case Instruction::FILL_ARRAY_DATA: {
705 ThrowException("Ljava/lang/NullPointerException;", nullptr,
706 "Attempt to write to null array");
707 break;
708 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100709 case Instruction::MONITOR_ENTER:
710 case Instruction::MONITOR_EXIT: {
711 ThrowException("Ljava/lang/NullPointerException;", nullptr,
712 "Attempt to do a synchronize operation on a null object");
713 break;
714 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800715 default: {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000716 const DexFile* dex_file =
717 method->GetDeclaringClass()->GetDexCache()->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100718 LOG(FATAL) << "NullPointerException at an unexpected instruction: "
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800719 << instr.DumpString(dex_file)
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100720 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700721 << method->PrettyMethod();
Ian Rogers62d6c772013-02-27 08:32:07 -0800722 break;
723 }
724 }
725}
726
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000727void ThrowNullPointerException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700728 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800729}
730
Orion Hodson928033d2018-02-07 05:30:54 +0000731// ReadOnlyBufferException
732
733void ThrowReadOnlyBufferException() {
734 Thread::Current()->ThrowNewException("Ljava/nio/ReadOnlyBufferException;", nullptr);
735}
736
Ian Rogers62d6c772013-02-27 08:32:07 -0800737// RuntimeException
738
739void ThrowRuntimeException(const char* fmt, ...) {
740 va_list args;
741 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700742 ThrowException("Ljava/lang/RuntimeException;", nullptr, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800743 va_end(args);
744}
745
Leonard Mosescueb842212016-10-06 17:26:36 -0700746// SecurityException
747
748void ThrowSecurityException(const char* fmt, ...) {
749 va_list args;
750 va_start(args, fmt);
751 ThrowException("Ljava/lang/SecurityException;", nullptr, fmt, &args);
752 va_end(args);
753}
754
Andreas Gampe103992b2016-01-04 15:32:43 -0800755// Stack overflow.
756
757void ThrowStackOverflowError(Thread* self) {
758 if (self->IsHandlingStackOverflow()) {
759 LOG(ERROR) << "Recursive stack overflow.";
760 // We don't fail here because SetStackEndForStackOverflow will print better diagnostics.
761 }
762
763 self->SetStackEndForStackOverflow(); // Allow space on the stack for constructor to execute.
764 JNIEnvExt* env = self->GetJniEnv();
765 std::string msg("stack size ");
766 msg += PrettySize(self->GetStackSize());
767
768 // Avoid running Java code for exception initialization.
769 // TODO: Checks to make this a bit less brittle.
770
771 std::string error_msg;
772
773 // Allocate an uninitialized object.
774 ScopedLocalRef<jobject> exc(env,
775 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
776 if (exc.get() != nullptr) {
777 // "Initialize".
778 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
779 // Only Throwable has "custom" fields:
780 // String detailMessage.
781 // Throwable cause (= this).
782 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
783 // Object stackState;
784 // StackTraceElement[] stackTrace;
785 // Only Throwable has a non-empty constructor:
786 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
787 // fillInStackTrace();
788
789 // detailMessage.
790 // TODO: Use String::FromModifiedUTF...?
791 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
792 if (s.get() != nullptr) {
793 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
794
795 // cause.
796 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
797
798 // suppressedExceptions.
799 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
800 WellKnownClasses::java_util_Collections,
801 WellKnownClasses::java_util_Collections_EMPTY_LIST));
802 CHECK(emptylist.get() != nullptr);
803 env->SetObjectField(exc.get(),
804 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
805 emptylist.get());
806
807 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
808 // nativeFillInStackTrace.
809 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
810 {
811 ScopedObjectAccessUnchecked soa(env);
812 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
813 }
814 if (stack_state_val.get() != nullptr) {
815 env->SetObjectField(exc.get(),
816 WellKnownClasses::java_lang_Throwable_stackState,
817 stack_state_val.get());
818
819 // stackTrace.
820 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
821 WellKnownClasses::libcore_util_EmptyArray,
822 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
823 env->SetObjectField(exc.get(),
824 WellKnownClasses::java_lang_Throwable_stackTrace,
825 stack_trace_elem.get());
826 } else {
827 error_msg = "Could not create stack trace.";
828 }
829 // Throw the exception.
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700830 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
Andreas Gampe103992b2016-01-04 15:32:43 -0800831 } else {
832 // Could not allocate a string object.
833 error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
834 }
835 } else {
836 error_msg = "Could not allocate StackOverflowError object.";
837 }
838
839 if (!error_msg.empty()) {
840 LOG(WARNING) << error_msg;
841 CHECK(self->IsExceptionPending());
842 }
843
844 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
845 self->ResetDefaultStackEnd(); // Return to default stack size.
846
847 // And restore protection if implicit checks are on.
848 if (!explicit_overflow_check) {
849 self->ProtectStack();
850 }
851}
852
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100853// StringIndexOutOfBoundsException
854
855void ThrowStringIndexOutOfBoundsException(int index, int length) {
856 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
857 StringPrintf("length=%d; index=%d", length, index).c_str());
858}
859
Orion Hodson928033d2018-02-07 05:30:54 +0000860// UnsupportedOperationException
861
862void ThrowUnsupportedOperationException() {
863 ThrowException("Ljava/lang/UnsupportedOperationException;");
864}
865
Ian Rogers62d6c772013-02-27 08:32:07 -0800866// VerifyError
867
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700868void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800869 va_list args;
870 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000871 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800872 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700873}
874
Narayan Kamath208f8572016-08-03 12:46:58 +0100875// WrongMethodTypeException
876
Orion Hodsona5dca522018-02-27 12:42:11 +0000877void ThrowWrongMethodTypeException(ObjPtr<mirror::MethodType> expected_type,
878 ObjPtr<mirror::MethodType> actual_type) {
Narayan Kamath208f8572016-08-03 12:46:58 +0100879 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;",
880 nullptr,
Narayan Kamath3e0dce02016-10-31 13:55:55 +0000881 StringPrintf("Expected %s but was %s",
Orion Hodson928033d2018-02-07 05:30:54 +0000882 expected_type->PrettyDescriptor().c_str(),
883 actual_type->PrettyDescriptor().c_str()).c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100884}
885
Ian Rogers87e552d2012-08-31 15:54:48 -0700886} // namespace art