blob: a1168afd1cfc02d5464c5c756900a4d3c3d063eb [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"
Vladimir Markof5c537e2018-04-09 18:33:55 +010027#include "debug_print.h"
David Sehr9e734c72018-01-04 17:56:19 -080028#include "dex/dex_file-inl.h"
29#include "dex/dex_instruction-inl.h"
David Sehr8c0961f2018-01-23 16:11:38 -080030#include "dex/invoke_type.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070031#include "mirror/class-inl.h"
Narayan Kamath208f8572016-08-03 12:46:58 +010032#include "mirror/method_type.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080033#include "mirror/object-inl.h"
34#include "mirror/object_array-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070035#include "nativehelper/scoped_local_ref.h"
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070036#include "obj_ptr-inl.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070037#include "thread.h"
Andreas Gampea7c83ac2017-09-11 08:14:23 -070038#include "well_known_classes.h"
Ian Rogers87e552d2012-08-31 15:54:48 -070039
Ian Rogers87e552d2012-08-31 15:54:48 -070040namespace art {
41
Andreas Gampe46ee31b2016-12-14 10:11:49 -080042using android::base::StringAppendV;
43using android::base::StringPrintf;
44
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070045static void AddReferrerLocation(std::ostream& os, ObjPtr<mirror::Class> referrer)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070046 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070047 if (referrer != nullptr) {
Mathieu Chartierf8322842014-05-16 10:59:25 -070048 std::string location(referrer->GetLocation());
Ian Rogers87e552d2012-08-31 15:54:48 -070049 if (!location.empty()) {
David Sehr709b0702016-10-13 09:12:37 -070050 os << " (declaration of '" << referrer->PrettyDescriptor()
51 << "' appears in " << location << ")";
Ian Rogers87e552d2012-08-31 15:54:48 -070052 }
53 }
54}
55
Orion Hodson928033d2018-02-07 05:30:54 +000056static void ThrowException(const char* exception_descriptor) REQUIRES_SHARED(Locks::mutator_lock_) {
57 Thread* self = Thread::Current();
58 self->ThrowNewException(exception_descriptor, nullptr);
59}
60
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000061static void ThrowException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070062 ObjPtr<mirror::Class> referrer,
63 const char* fmt,
64 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070065 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers87e552d2012-08-31 15:54:48 -070066 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070067 if (args != nullptr) {
Ian Rogers62d6c772013-02-27 08:32:07 -080068 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->ThrowNewException(exception_descriptor, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -070077}
78
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000079static void ThrowWrappedException(const char* exception_descriptor,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070080 ObjPtr<mirror::Class> referrer,
81 const char* fmt,
82 va_list* args = nullptr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070083 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe329d1882014-04-08 10:32:19 -070084 std::ostringstream msg;
Mathieu Chartier2cebb242015-04-21 16:50:40 -070085 if (args != nullptr) {
Andreas Gampe329d1882014-04-08 10:32:19 -070086 std::string vmsg;
87 StringAppendV(&vmsg, fmt, *args);
88 msg << vmsg;
89 } else {
90 msg << fmt;
91 }
92 AddReferrerLocation(msg, referrer);
93 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000094 self->ThrowNewWrappedException(exception_descriptor, msg.str().c_str());
Andreas Gampe329d1882014-04-08 10:32:19 -070095}
96
Sebastien Hertz56adf602013-07-09 17:27:07 +020097// AbstractMethodError
98
Mathieu Chartiere401d142015-04-22 13:56:20 -070099void ThrowAbstractMethodError(ArtMethod* method) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700100 ThrowException("Ljava/lang/AbstractMethodError;", nullptr,
Sebastien Hertz56adf602013-07-09 17:27:07 +0200101 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700102 ArtMethod::PrettyMethod(method).c_str()).c_str());
Sebastien Hertz56adf602013-07-09 17:27:07 +0200103}
104
Alex Light705ad492015-09-21 11:36:30 -0700105void ThrowAbstractMethodError(uint32_t method_idx, const DexFile& dex_file) {
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700106 ThrowException("Ljava/lang/AbstractMethodError;", /* referrer= */ nullptr,
Alex Light705ad492015-09-21 11:36:30 -0700107 StringPrintf("abstract method \"%s\"",
David Sehr709b0702016-10-13 09:12:37 -0700108 dex_file.PrettyMethod(method_idx,
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700109 /* with_signature= */ true).c_str()).c_str());
Alex Light705ad492015-09-21 11:36:30 -0700110}
111
Ian Rogers62d6c772013-02-27 08:32:07 -0800112// ArithmeticException
113
Sebastien Hertz0a3b8632013-06-26 11:16:01 +0200114void ThrowArithmeticExceptionDivideByZero() {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700115 ThrowException("Ljava/lang/ArithmeticException;", nullptr, "divide by zero");
Ian Rogers62d6c772013-02-27 08:32:07 -0800116}
117
118// ArrayIndexOutOfBoundsException
119
120void ThrowArrayIndexOutOfBoundsException(int index, int length) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700121 ThrowException("Ljava/lang/ArrayIndexOutOfBoundsException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800122 StringPrintf("length=%d; index=%d", length, index).c_str());
123}
124
125// ArrayStoreException
126
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700127void ThrowArrayStoreException(ObjPtr<mirror::Class> element_class,
128 ObjPtr<mirror::Class> array_class) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700129 ThrowException("Ljava/lang/ArrayStoreException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800130 StringPrintf("%s cannot be stored in an array of type %s",
David Sehr709b0702016-10-13 09:12:37 -0700131 mirror::Class::PrettyDescriptor(element_class).c_str(),
132 mirror::Class::PrettyDescriptor(array_class).c_str()).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800133}
134
Orion Hodsonc069a302017-01-18 09:23:12 +0000135// BootstrapMethodError
136
137void ThrowBootstrapMethodError(const char* fmt, ...) {
138 va_list args;
139 va_start(args, fmt);
140 ThrowException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
141 va_end(args);
142}
143
144void ThrowWrappedBootstrapMethodError(const char* fmt, ...) {
145 va_list args;
146 va_start(args, fmt);
147 ThrowWrappedException("Ljava/lang/BootstrapMethodError;", nullptr, fmt, &args);
148 va_end(args);
149}
150
Ian Rogers62d6c772013-02-27 08:32:07 -0800151// ClassCastException
152
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700153void ThrowClassCastException(ObjPtr<mirror::Class> dest_type, ObjPtr<mirror::Class> src_type) {
Vladimir Markof5c537e2018-04-09 18:33:55 +0100154 DumpB77342775DebugData(dest_type, 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) << "'";
Vladimir Markof5c537e2018-04-09 18:33:55 +0100282 DumpB77342775DebugData(target_class, this_object->GetClass());
Alex Light705ad492015-09-21 11:36:30 -0700283 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
284 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
285 msg.str().c_str());
286}
287
Mathieu Chartiere401d142015-04-22 13:56:20 -0700288void ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(ArtMethod* interface_method,
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700289 ObjPtr<mirror::Object> this_object,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700290 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700291 // Referrer is calling interface_method on this_object, however, the interface_method isn't
292 // implemented by this_object.
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700293 CHECK(this_object != nullptr);
Ian Rogers87e552d2012-08-31 15:54:48 -0700294 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700295 msg << "Class '" << mirror::Class::PrettyDescriptor(this_object->GetClass())
Ian Rogers87e552d2012-08-31 15:54:48 -0700296 << "' does not implement interface '"
David Sehr709b0702016-10-13 09:12:37 -0700297 << mirror::Class::PrettyDescriptor(interface_method->GetDeclaringClass())
298 << "' in call to '" << ArtMethod::PrettyMethod(interface_method) << "'";
Vladimir Markof5c537e2018-04-09 18:33:55 +0100299 DumpB77342775DebugData(interface_method->GetDeclaringClass(), this_object->GetClass());
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000300 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Mathieu Chartiere401d142015-04-22 13:56:20 -0700301 referrer != nullptr ? referrer->GetDeclaringClass() : nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800302 msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700303}
304
Mathieu Chartierc7853442015-03-27 14:35:38 -0700305void ThrowIncompatibleClassChangeErrorField(ArtField* resolved_field, bool is_static,
Mathieu Chartiere401d142015-04-22 13:56:20 -0700306 ArtMethod* referrer) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700307 std::ostringstream msg;
David Sehr709b0702016-10-13 09:12:37 -0700308 msg << "Expected '" << ArtField::PrettyField(resolved_field) << "' to be a "
Ian Rogersb726dcb2012-09-05 08:57:23 -0700309 << (is_static ? "static" : "instance") << " field" << " rather than a "
310 << (is_static ? "instance" : "static") << " field";
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer->GetDeclaringClass(),
Ian Rogers62d6c772013-02-27 08:32:07 -0800312 msg.str().c_str());
313}
314
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700315void ThrowIncompatibleClassChangeError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800316 va_list args;
317 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000318 ThrowException("Ljava/lang/IncompatibleClassChangeError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800319 va_end(args);
320}
321
Alex Light9139e002015-10-09 15:59:48 -0700322void ThrowIncompatibleClassChangeErrorForMethodConflict(ArtMethod* method) {
323 DCHECK(method != nullptr);
324 ThrowException("Ljava/lang/IncompatibleClassChangeError;",
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700325 /*referrer=*/nullptr,
Alex Light9139e002015-10-09 15:59:48 -0700326 StringPrintf("Conflicting default method implementations %s",
David Sehr709b0702016-10-13 09:12:37 -0700327 ArtMethod::PrettyMethod(method).c_str()).c_str());
Alex Light9139e002015-10-09 15:59:48 -0700328}
329
Orion Hodson928033d2018-02-07 05:30:54 +0000330// IndexOutOfBoundsException
331
332void ThrowIndexOutOfBoundsException(int index, int length) {
333 ThrowException("Ljava/lang/IndexOutOfBoundsException;", nullptr,
334 StringPrintf("length=%d; index=%d", length, index).c_str());
335}
336
Alex Lightdb01a092017-04-03 15:39:55 -0700337// InternalError
338
339void ThrowInternalError(const char* fmt, ...) {
340 va_list args;
341 va_start(args, fmt);
342 ThrowException("Ljava/lang/InternalError;", nullptr, fmt, &args);
343 va_end(args);
344}
Alex Light9139e002015-10-09 15:59:48 -0700345
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700346// IOException
347
348void ThrowIOException(const char* fmt, ...) {
349 va_list args;
350 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700351 ThrowException("Ljava/io/IOException;", nullptr, fmt, &args);
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700352 va_end(args);
353}
354
Andreas Gampe329d1882014-04-08 10:32:19 -0700355void ThrowWrappedIOException(const char* fmt, ...) {
356 va_list args;
357 va_start(args, fmt);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700358 ThrowWrappedException("Ljava/io/IOException;", nullptr, fmt, &args);
Andreas Gampe329d1882014-04-08 10:32:19 -0700359 va_end(args);
360}
361
Ian Rogers62d6c772013-02-27 08:32:07 -0800362// LinkageError
363
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700364void ThrowLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800365 va_list args;
366 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000367 ThrowException("Ljava/lang/LinkageError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800368 va_end(args);
369}
370
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700371void ThrowWrappedLinkageError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Vladimir Markod5e5a0e2015-05-08 12:26:59 +0100372 va_list args;
373 va_start(args, fmt);
374 ThrowWrappedException("Ljava/lang/LinkageError;", referrer, fmt, &args);
375 va_end(args);
376}
377
Ian Rogers62d6c772013-02-27 08:32:07 -0800378// NegativeArraySizeException
379
380void ThrowNegativeArraySizeException(int size) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700381 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr,
Brian Carlstromea46f952013-07-30 01:26:50 -0700382 StringPrintf("%d", size).c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800383}
384
385void ThrowNegativeArraySizeException(const char* msg) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700386 ThrowException("Ljava/lang/NegativeArraySizeException;", nullptr, msg);
Ian Rogers62d6c772013-02-27 08:32:07 -0800387}
388
389// NoSuchFieldError
390
Vladimir Marko72101082019-02-05 16:16:30 +0000391void ThrowNoSuchFieldError(std::string_view scope,
392 ObjPtr<mirror::Class> c,
393 std::string_view type,
394 std::string_view name) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800395 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700396 std::string temp;
Ian Rogers62d6c772013-02-27 08:32:07 -0800397 msg << "No " << scope << "field " << name << " of type " << type
Ian Rogers1ff3c982014-08-12 02:30:58 -0700398 << " in class " << c->GetDescriptor(&temp) << " or its superclasses";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000399 ThrowException("Ljava/lang/NoSuchFieldError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700400}
401
Vladimir Marko72101082019-02-05 16:16:30 +0000402void ThrowNoSuchFieldException(ObjPtr<mirror::Class> c, std::string_view name) {
Mathieu Chartier4e067782015-05-13 13:13:24 -0700403 std::ostringstream msg;
404 std::string temp;
405 msg << "No field " << name << " in class " << c->GetDescriptor(&temp);
406 ThrowException("Ljava/lang/NoSuchFieldException;", c, msg.str().c_str());
407}
408
Ian Rogers87e552d2012-08-31 15:54:48 -0700409// NoSuchMethodError
410
Vladimir Marko72101082019-02-05 16:16:30 +0000411void ThrowNoSuchMethodError(InvokeType type,
412 ObjPtr<mirror::Class> c,
413 std::string_view name,
Ian Rogersd91d6d62013-09-25 20:26:14 -0700414 const Signature& signature) {
Ian Rogers87e552d2012-08-31 15:54:48 -0700415 std::ostringstream msg;
Ian Rogers1ff3c982014-08-12 02:30:58 -0700416 std::string temp;
Ian Rogers87e552d2012-08-31 15:54:48 -0700417 msg << "No " << type << " method " << name << signature
Ian Rogers1ff3c982014-08-12 02:30:58 -0700418 << " in class " << c->GetDescriptor(&temp) << " or its super classes";
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000419 ThrowException("Ljava/lang/NoSuchMethodError;", c, msg.str().c_str());
Ian Rogers87e552d2012-08-31 15:54:48 -0700420}
421
Ian Rogers62d6c772013-02-27 08:32:07 -0800422// NullPointerException
423
Mathieu Chartierc7853442015-03-27 14:35:38 -0700424void ThrowNullPointerExceptionForFieldAccess(ArtField* field, bool is_read) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800425 std::ostringstream msg;
426 msg << "Attempt to " << (is_read ? "read from" : "write to")
David Sehr709b0702016-10-13 09:12:37 -0700427 << " field '" << ArtField::PrettyField(field, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700428 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800429}
430
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000431static void ThrowNullPointerExceptionForMethodAccessImpl(uint32_t method_idx,
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200432 const DexFile& dex_file,
433 InvokeType type)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700434 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800435 std::ostringstream msg;
436 msg << "Attempt to invoke " << type << " method '"
David Sehr709b0702016-10-13 09:12:37 -0700437 << dex_file.PrettyMethod(method_idx, true) << "' on a null object reference";
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700438 ThrowException("Ljava/lang/NullPointerException;", nullptr, msg.str().c_str());
Ian Rogers62d6c772013-02-27 08:32:07 -0800439}
440
Vladimir Marko7e097372018-11-28 16:40:59 +0000441void ThrowNullPointerExceptionForMethodAccess(uint32_t method_idx, InvokeType type) {
442 const DexFile& dex_file = *Thread::Current()->GetCurrentMethod(nullptr)->GetDexFile();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000443 ThrowNullPointerExceptionForMethodAccessImpl(method_idx, dex_file, type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200444}
445
Vladimir Marko7e097372018-11-28 16:40:59 +0000446void ThrowNullPointerExceptionForMethodAccess(ArtMethod* method, InvokeType type) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000447 ThrowNullPointerExceptionForMethodAccessImpl(method->GetDexMethodIndex(),
Vladimir Marko7e097372018-11-28 16:40:59 +0000448 *method->GetDexFile(),
449 type);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200450}
451
Vladimir Marko953437b2016-08-24 08:30:46 +0000452static bool IsValidReadBarrierImplicitCheck(uintptr_t addr) {
453 DCHECK(kEmitCompilerReadBarrier);
454 uint32_t monitor_offset = mirror::Object::MonitorOffset().Uint32Value();
Vladimir Marko33bff252017-11-01 14:35:42 +0000455 if (kUseBakerReadBarrier &&
456 (kRuntimeISA == InstructionSet::kX86 || kRuntimeISA == InstructionSet::kX86_64)) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000457 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
458 monitor_offset += gray_byte_position;
459 }
460 return addr == monitor_offset;
461}
462
Nicolas Geoffray13449142017-12-07 22:26:24 +0000463static bool IsValidImplicitCheck(uintptr_t addr, const Instruction& instr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700464 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100465 if (!CanDoImplicitNullCheckOn(addr)) {
466 return false;
467 }
468
469 switch (instr.Opcode()) {
470 case Instruction::INVOKE_DIRECT:
471 case Instruction::INVOKE_DIRECT_RANGE:
472 case Instruction::INVOKE_VIRTUAL:
473 case Instruction::INVOKE_VIRTUAL_RANGE:
474 case Instruction::INVOKE_INTERFACE:
475 case Instruction::INVOKE_INTERFACE_RANGE:
Orion Hodsonac141392017-01-13 11:53:47 +0000476 case Instruction::INVOKE_POLYMORPHIC:
477 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100478 case Instruction::INVOKE_VIRTUAL_QUICK:
479 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
480 // Without inlining, we could just check that the offset is the class offset.
481 // However, when inlining, the compiler can (validly) merge the null check with a field access
482 // on the same object. Note that the stack map at the NPE will reflect the invoke's location,
483 // which is the caller.
484 return true;
485 }
486
Vladimir Marko953437b2016-08-24 08:30:46 +0000487 case Instruction::IGET_OBJECT:
488 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
489 return true;
490 }
491 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100492 case Instruction::IGET:
493 case Instruction::IGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100494 case Instruction::IGET_BOOLEAN:
495 case Instruction::IGET_BYTE:
496 case Instruction::IGET_CHAR:
497 case Instruction::IGET_SHORT:
498 case Instruction::IPUT:
499 case Instruction::IPUT_WIDE:
500 case Instruction::IPUT_OBJECT:
501 case Instruction::IPUT_BOOLEAN:
502 case Instruction::IPUT_BYTE:
503 case Instruction::IPUT_CHAR:
504 case Instruction::IPUT_SHORT: {
Nicolas Geoffray13449142017-12-07 22:26:24 +0000505 // We might be doing an implicit null check with an offset that doesn't correspond
506 // to the instruction, for example with two field accesses and the first one being
507 // eliminated or re-ordered.
508 return true;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100509 }
510
Vladimir Marko953437b2016-08-24 08:30:46 +0000511 case Instruction::IGET_OBJECT_QUICK:
512 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
513 return true;
514 }
515 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100516 case Instruction::IGET_QUICK:
517 case Instruction::IGET_BOOLEAN_QUICK:
518 case Instruction::IGET_BYTE_QUICK:
519 case Instruction::IGET_CHAR_QUICK:
520 case Instruction::IGET_SHORT_QUICK:
521 case Instruction::IGET_WIDE_QUICK:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100522 case Instruction::IPUT_QUICK:
523 case Instruction::IPUT_BOOLEAN_QUICK:
524 case Instruction::IPUT_BYTE_QUICK:
525 case Instruction::IPUT_CHAR_QUICK:
526 case Instruction::IPUT_SHORT_QUICK:
527 case Instruction::IPUT_WIDE_QUICK:
528 case Instruction::IPUT_OBJECT_QUICK: {
Nicolas Geoffray13449142017-12-07 22:26:24 +0000529 // We might be doing an implicit null check with an offset that doesn't correspond
530 // to the instruction, for example with two field accesses and the first one being
531 // eliminated or re-ordered.
532 return true;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100533 }
534
Vladimir Marko953437b2016-08-24 08:30:46 +0000535 case Instruction::AGET_OBJECT:
536 if (kEmitCompilerReadBarrier && IsValidReadBarrierImplicitCheck(addr)) {
537 return true;
538 }
539 FALLTHROUGH_INTENDED;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100540 case Instruction::AGET:
541 case Instruction::AGET_WIDE:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100542 case Instruction::AGET_BOOLEAN:
543 case Instruction::AGET_BYTE:
544 case Instruction::AGET_CHAR:
545 case Instruction::AGET_SHORT:
546 case Instruction::APUT:
547 case Instruction::APUT_WIDE:
548 case Instruction::APUT_OBJECT:
549 case Instruction::APUT_BOOLEAN:
550 case Instruction::APUT_BYTE:
551 case Instruction::APUT_CHAR:
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100552 case Instruction::APUT_SHORT:
553 case Instruction::FILL_ARRAY_DATA:
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100554 case Instruction::ARRAY_LENGTH: {
Nicolas Geoffray350cc992016-06-29 21:45:10 +0100555 // The length access should crash. We currently do not do implicit checks on
556 // the array access itself.
Vladimir Marko953437b2016-08-24 08:30:46 +0000557 return (addr == 0u) || (addr == mirror::Array::LengthOffset().Uint32Value());
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100558 }
559
560 default: {
561 // We have covered all the cases where an NPE could occur.
562 // Note that this must be kept in sync with the compiler, and adding
563 // any new way to do implicit checks in the compiler should also update
564 // this code.
565 return false;
566 }
567 }
568}
569
570void ThrowNullPointerExceptionFromDexPC(bool check_address, uintptr_t addr) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000571 uint32_t throw_dex_pc;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700572 ArtMethod* method = Thread::Current()->GetCurrentMethod(&throw_dex_pc);
David Sehr0225f8e2018-01-31 08:52:24 +0000573 CodeItemInstructionAccessor accessor(method->DexInstructions());
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800574 CHECK_LT(throw_dex_pc, accessor.InsnsSizeInCodeUnits());
575 const Instruction& instr = accessor.InstructionAt(throw_dex_pc);
576 if (check_address && !IsValidImplicitCheck(addr, instr)) {
Vladimir Marko7e097372018-11-28 16:40:59 +0000577 const DexFile* dex_file = method->GetDexFile();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100578 LOG(FATAL) << "Invalid address for an implicit NullPointerException check: "
579 << "0x" << std::hex << addr << std::dec
580 << ", at "
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800581 << instr.DumpString(dex_file)
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100582 << " in "
David Sehr709b0702016-10-13 09:12:37 -0700583 << method->PrettyMethod();
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100584 }
585
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800586 switch (instr.Opcode()) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800587 case Instruction::INVOKE_DIRECT:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800588 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kDirect);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200589 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800590 case Instruction::INVOKE_DIRECT_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800591 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kDirect);
Ian Rogers62d6c772013-02-27 08:32:07 -0800592 break;
593 case Instruction::INVOKE_VIRTUAL:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800594 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kVirtual);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200595 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800596 case Instruction::INVOKE_VIRTUAL_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800597 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kVirtual);
Ian Rogers62d6c772013-02-27 08:32:07 -0800598 break;
599 case Instruction::INVOKE_INTERFACE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800600 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_35c(), kInterface);
Sebastien Hertz75b2a4a2013-05-21 09:25:10 +0200601 break;
Ian Rogers62d6c772013-02-27 08:32:07 -0800602 case Instruction::INVOKE_INTERFACE_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800603 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_3rc(), kInterface);
Ian Rogers62d6c772013-02-27 08:32:07 -0800604 break;
Orion Hodsonac141392017-01-13 11:53:47 +0000605 case Instruction::INVOKE_POLYMORPHIC:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800606 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_45cc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000607 break;
608 case Instruction::INVOKE_POLYMORPHIC_RANGE:
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800609 ThrowNullPointerExceptionForMethodAccess(instr.VRegB_4rcc(), kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +0000610 break;
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200611 case Instruction::INVOKE_VIRTUAL_QUICK:
612 case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000613 uint16_t method_idx = method->GetIndexFromQuickening(throw_dex_pc);
614 if (method_idx != DexFile::kDexNoIndex16) {
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200615 // NPE with precise message.
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000616 ThrowNullPointerExceptionForMethodAccess(method_idx, kVirtual);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200617 } else {
618 // NPE with imprecise message.
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000619 ThrowNullPointerException("Attempt to invoke a virtual method on a null object reference");
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200620 }
621 break;
622 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800623 case Instruction::IGET:
624 case Instruction::IGET_WIDE:
625 case Instruction::IGET_OBJECT:
626 case Instruction::IGET_BOOLEAN:
627 case Instruction::IGET_BYTE:
628 case Instruction::IGET_CHAR:
629 case Instruction::IGET_SHORT: {
Mathieu Chartierc7853442015-03-27 14:35:38 -0700630 ArtField* field =
Mathieu Chartier808c7a52017-12-15 11:19:33 -0800631 Runtime::Current()->GetClassLinker()->ResolveField(instr.VRegC_22c(), method, false);
Andreas Gampe71307442018-02-06 13:38:03 -0800632 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700633 ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ true);
Ian Rogers62d6c772013-02-27 08:32:07 -0800634 break;
635 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200636 case Instruction::IGET_QUICK:
Mathieu Chartierffc605c2014-12-10 10:35:44 -0800637 case Instruction::IGET_BOOLEAN_QUICK:
638 case Instruction::IGET_BYTE_QUICK:
639 case Instruction::IGET_CHAR_QUICK:
640 case Instruction::IGET_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200641 case Instruction::IGET_WIDE_QUICK:
642 case Instruction::IGET_OBJECT_QUICK: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000643 uint16_t field_idx = method->GetIndexFromQuickening(throw_dex_pc);
644 ArtField* field = nullptr;
645 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
646 field = Runtime::Current()->GetClassLinker()->ResolveField(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700647 field_idx, method, /* is_static= */ false);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000648 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700649 ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ true);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200650 break;
651 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800652 case Instruction::IPUT:
653 case Instruction::IPUT_WIDE:
654 case Instruction::IPUT_OBJECT:
655 case Instruction::IPUT_BOOLEAN:
656 case Instruction::IPUT_BYTE:
657 case Instruction::IPUT_CHAR:
658 case Instruction::IPUT_SHORT: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000659 ArtField* field = Runtime::Current()->GetClassLinker()->ResolveField(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700660 instr.VRegC_22c(), method, /* is_static= */ false);
Andreas Gampe71307442018-02-06 13:38:03 -0800661 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700662 ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ false);
Ian Rogers62d6c772013-02-27 08:32:07 -0800663 break;
664 }
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200665 case Instruction::IPUT_QUICK:
Fred Shih37f05ef2014-07-16 18:38:08 -0700666 case Instruction::IPUT_BOOLEAN_QUICK:
667 case Instruction::IPUT_BYTE_QUICK:
668 case Instruction::IPUT_CHAR_QUICK:
669 case Instruction::IPUT_SHORT_QUICK:
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200670 case Instruction::IPUT_WIDE_QUICK:
671 case Instruction::IPUT_OBJECT_QUICK: {
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000672 uint16_t field_idx = method->GetIndexFromQuickening(throw_dex_pc);
673 ArtField* field = nullptr;
674 CHECK_NE(field_idx, DexFile::kDexNoIndex16);
675 field = Runtime::Current()->GetClassLinker()->ResolveField(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700676 field_idx, method, /* is_static= */ false);
Nicolas Geoffrayb041a402017-11-13 15:16:22 +0000677 Thread::Current()->ClearException(); // Resolution may fail, ignore.
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700678 ThrowNullPointerExceptionForFieldAccess(field, /* is_read= */ false);
Sebastien Hertz2d6ba512013-05-17 11:31:37 +0200679 break;
680 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800681 case Instruction::AGET:
682 case Instruction::AGET_WIDE:
683 case Instruction::AGET_OBJECT:
684 case Instruction::AGET_BOOLEAN:
685 case Instruction::AGET_BYTE:
686 case Instruction::AGET_CHAR:
687 case Instruction::AGET_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700688 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800689 "Attempt to read from null array");
690 break;
691 case Instruction::APUT:
692 case Instruction::APUT_WIDE:
693 case Instruction::APUT_OBJECT:
694 case Instruction::APUT_BOOLEAN:
695 case Instruction::APUT_BYTE:
696 case Instruction::APUT_CHAR:
697 case Instruction::APUT_SHORT:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700698 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800699 "Attempt to write to null array");
700 break;
701 case Instruction::ARRAY_LENGTH:
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700702 ThrowException("Ljava/lang/NullPointerException;", nullptr,
Ian Rogers62d6c772013-02-27 08:32:07 -0800703 "Attempt to get length of null array");
704 break;
Nicolas Geoffraye8e11272016-06-28 18:08:46 +0100705 case Instruction::FILL_ARRAY_DATA: {
706 ThrowException("Ljava/lang/NullPointerException;", nullptr,
707 "Attempt to write to null array");
708 break;
709 }
Nicolas Geoffray7f0ae732016-06-29 14:54:35 +0100710 case Instruction::MONITOR_ENTER:
711 case Instruction::MONITOR_EXIT: {
712 ThrowException("Ljava/lang/NullPointerException;", nullptr,
713 "Attempt to do a synchronize operation on a null object");
714 break;
715 }
Ian Rogers62d6c772013-02-27 08:32:07 -0800716 default: {
Vladimir Marko7e097372018-11-28 16:40:59 +0000717 const DexFile* dex_file = method->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();
Elliott Hughesc1896c92018-11-29 11:33:18 -0800722 UNREACHABLE();
Ian Rogers62d6c772013-02-27 08:32:07 -0800723 }
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.
Andreas Gampe5ad2e062018-10-01 15:35:47 +0000770 //
771 // Note: this lambda ensures that the destruction of the ScopedLocalRefs will run in the extended
772 // stack, which is important for modes with larger stack sizes (e.g., ASAN). Using a lambda
773 // instead of a block simplifies the control flow.
774 auto create_and_throw = [&]() REQUIRES_SHARED(Locks::mutator_lock_) {
775 // Allocate an uninitialized object.
776 ScopedLocalRef<jobject> exc(env,
777 env->AllocObject(WellKnownClasses::java_lang_StackOverflowError));
778 if (exc == nullptr) {
779 LOG(WARNING) << "Could not allocate StackOverflowError object.";
780 return;
781 }
Andreas Gampe103992b2016-01-04 15:32:43 -0800782
Andreas Gampe103992b2016-01-04 15:32:43 -0800783 // "Initialize".
784 // StackOverflowError -> VirtualMachineError -> Error -> Throwable -> Object.
785 // Only Throwable has "custom" fields:
786 // String detailMessage.
787 // Throwable cause (= this).
788 // List<Throwable> suppressedExceptions (= Collections.emptyList()).
789 // Object stackState;
790 // StackTraceElement[] stackTrace;
791 // Only Throwable has a non-empty constructor:
Neil Fullerc0f02d42018-06-11 09:49:01 +0000792 // this.stackTrace = EmptyArray.STACK_TRACE_ELEMENT;
Andreas Gampe103992b2016-01-04 15:32:43 -0800793 // fillInStackTrace();
794
795 // detailMessage.
796 // TODO: Use String::FromModifiedUTF...?
797 ScopedLocalRef<jstring> s(env, env->NewStringUTF(msg.c_str()));
Andreas Gampe5ad2e062018-10-01 15:35:47 +0000798 if (s == nullptr) {
799 LOG(WARNING) << "Could not throw new StackOverflowError because JNI NewStringUTF failed.";
800 return;
Andreas Gampe103992b2016-01-04 15:32:43 -0800801 }
Andreas Gampe103992b2016-01-04 15:32:43 -0800802
Andreas Gampe5ad2e062018-10-01 15:35:47 +0000803 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_detailMessage, s.get());
804
805 // cause.
806 env->SetObjectField(exc.get(), WellKnownClasses::java_lang_Throwable_cause, exc.get());
807
808 // suppressedExceptions.
809 ScopedLocalRef<jobject> emptylist(env, env->GetStaticObjectField(
810 WellKnownClasses::java_util_Collections,
811 WellKnownClasses::java_util_Collections_EMPTY_LIST));
812 CHECK(emptylist != nullptr);
813 env->SetObjectField(exc.get(),
814 WellKnownClasses::java_lang_Throwable_suppressedExceptions,
815 emptylist.get());
816
817 // stackState is set as result of fillInStackTrace. fillInStackTrace calls
818 // nativeFillInStackTrace.
819 ScopedLocalRef<jobject> stack_state_val(env, nullptr);
820 {
821 ScopedObjectAccessUnchecked soa(env); // TODO: Is this necessary?
822 stack_state_val.reset(soa.Self()->CreateInternalStackTrace<false>(soa));
823 }
824 if (stack_state_val != nullptr) {
825 env->SetObjectField(exc.get(),
826 WellKnownClasses::java_lang_Throwable_stackState,
827 stack_state_val.get());
828
829 // stackTrace.
830 ScopedLocalRef<jobject> stack_trace_elem(env, env->GetStaticObjectField(
831 WellKnownClasses::libcore_util_EmptyArray,
832 WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT));
833 env->SetObjectField(exc.get(),
834 WellKnownClasses::java_lang_Throwable_stackTrace,
835 stack_trace_elem.get());
836 } else {
837 LOG(WARNING) << "Could not create stack trace.";
838 // Note: we'll create an exception without stack state, which is valid.
839 }
840
841 // Throw the exception.
842 self->SetException(self->DecodeJObject(exc.get())->AsThrowable());
843 };
844 create_and_throw();
845 CHECK(self->IsExceptionPending());
Andreas Gampe103992b2016-01-04 15:32:43 -0800846
847 bool explicit_overflow_check = Runtime::Current()->ExplicitStackOverflowChecks();
848 self->ResetDefaultStackEnd(); // Return to default stack size.
849
850 // And restore protection if implicit checks are on.
851 if (!explicit_overflow_check) {
852 self->ProtectStack();
853 }
854}
855
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100856// StringIndexOutOfBoundsException
857
858void ThrowStringIndexOutOfBoundsException(int index, int length) {
859 ThrowException("Ljava/lang/StringIndexOutOfBoundsException;", nullptr,
860 StringPrintf("length=%d; index=%d", length, index).c_str());
861}
862
Orion Hodson928033d2018-02-07 05:30:54 +0000863// UnsupportedOperationException
864
865void ThrowUnsupportedOperationException() {
866 ThrowException("Ljava/lang/UnsupportedOperationException;");
867}
868
Ian Rogers62d6c772013-02-27 08:32:07 -0800869// VerifyError
870
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -0700871void ThrowVerifyError(ObjPtr<mirror::Class> referrer, const char* fmt, ...) {
Ian Rogers62d6c772013-02-27 08:32:07 -0800872 va_list args;
873 va_start(args, fmt);
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000874 ThrowException("Ljava/lang/VerifyError;", referrer, fmt, &args);
Ian Rogers62d6c772013-02-27 08:32:07 -0800875 va_end(args);
Ian Rogers87e552d2012-08-31 15:54:48 -0700876}
877
Narayan Kamath208f8572016-08-03 12:46:58 +0100878// WrongMethodTypeException
879
Orion Hodsona5dca522018-02-27 12:42:11 +0000880void ThrowWrongMethodTypeException(ObjPtr<mirror::MethodType> expected_type,
881 ObjPtr<mirror::MethodType> actual_type) {
Orion Hodson3f383462018-05-17 14:03:39 +0100882 ThrowWrongMethodTypeException(expected_type->PrettyDescriptor(), actual_type->PrettyDescriptor());
883}
884
885void ThrowWrongMethodTypeException(const std::string& expected_descriptor,
886 const std::string& actual_descriptor) {
887 std::ostringstream msg;
888 msg << "Expected " << expected_descriptor << " but was " << actual_descriptor;
889 ThrowException("Ljava/lang/invoke/WrongMethodTypeException;", nullptr, msg.str().c_str());
Narayan Kamath208f8572016-08-03 12:46:58 +0100890}
891
Ian Rogers87e552d2012-08-31 15:54:48 -0700892} // namespace art